Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I name variables dynamically in C#?

Is there a way to dynamically name variables?

What I need to do is take a list of variable names from an input file and create variables with those names. Is this possible?

Something like:

Variable <dynamic name of variable here> = new Variable(input);

Assume that I already have the Variable class taken care of, and the name of the variable is contain in a string called strLine.

like image 466
Brandon Avatar asked Feb 17 '11 19:02

Brandon


1 Answers

C# 4.0, using the dynamic objects:

dynamic d = new ExpandoObject();
((IDictionary<string, object>)d)["MyProperty"] =  5;
int val = d.MyProperty; // 5
like image 84
xanatos Avatar answered Oct 25 '22 07:10

xanatos