Dynamic variable in C#?

Moon picture Moon · Aug 15, 2009 · Viewed 12.9k times · Source

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#?

Answer

Martin v. Löwis picture Martin v. Löwis · Aug 15, 2009

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