For example, PHP code:
$test = "hello";
${$test} = $test;
echo $hello; // return hello
How to do this in C#? Thanks in advance.
UPD: Dynamic variable in C#? – here is an answer.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This isn’t supported in C#. You could use an
ExpandoObjectand set a member on it, but it’s not quite the same as the PHP code. You’ll still need to refer to theExpandoObjectby a variable name.Nonetheless, this doesn’t help with code clarity. If all you want to do is map a name to a value you can use a
Dictionary, which is really whatExpandoObjectuses internally, as demonstrated by the cast in the code above.