Search code examples
c#dynamic-variables

Dynamic variable in C#?


Is it possible to use a dynamic variable (not sure about naming) in C#?

In PHP, I can do

$var_1 = "2";
$var_2 = "this is variable 2";

$test = ${"var_".$var_1};

echo $test;

output: this is variable 2;

Can we do this in C#?


Solution

  • In C#, you use dictionaries to associate values with strings.