-3

I am new in C#, I use to program PHP.

I want to do same thing that is done in PHP code below for C#.

  <?php
     for($i=0;$i<10;$i++)
    {
     $tempVar="$newVar".$i;

     echo $$tempVar; //to print the content of $newVar0 to $newvar9       
    }
  ?>

Is the same option, of changing variable name dynamically, available in C#. Please Guide. Thank you

2
  • see if System.Diagnostics.Debug.Print is what you're looking for Commented Nov 1, 2015 at 5:05
  • Does this answer your question? Dynamic variable in C#? Commented Sep 15, 2022 at 19:50

1 Answer 1

1

PHP and C# both have very different syntax. You must start learning by yourself from online tutorials. Your answer is

var newVar ="This is test";
var var1= newVar;
Console.WriteLine(var1);

Tutorial Sources (learn step by step)

http://www.tutorialspoint.com/csharp/

http://techfunda.com/Howto/c

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, Thank you for reply. I have changed the example PHP code, I hope this can explain clearly my question.
This is very old, but it does not answer the question. The question pertains to PHP's Variable variables, where you can access a variable via a variable string, for instance. It is not simply asking the syntax in c# to access another variable. See: php.net/manual/en/language.variables.variable.php (particularly Example #1 Variable property example) In the user's code you see "$newVar".$i which is being used to access a variable $newVar0 - your answer does not explain how to create or access variables dynamically - only how to create and access them statically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.