-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

1 Answers1

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

Bhavik Patel
  • 1,466
  • 1
  • 12
  • 28
  • Hi, Thank you for reply. I have changed the example PHP code, I hope this can explain clearly my question. – Santosh Dahal Nov 02 '15 at 05:27
  • 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: https://www.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. – zbee Sep 15 '22 at 19:48