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 p
Variable names should be known at compile time. If you intend to populate those names dynamically at runtime you could use a List<T>.
var variables = List<Variable>();
variables.Add(new Variable { Name = input1 });
variables.Add(new Variable { Name = input2 });
...
C# 4.0, using the dynamic
objects:
dynamic d = new ExpandoObject();
((IDictionary<string, object>)d)["MyProperty"] = 5;
int val = d.MyProperty; // 5
No, but you could use a Dictionary<string, Variable>
, and then you can refer to each variable by its quoted name.