Does .NET natively support anything similar to PHP’s variable variables?
If not, how1 could such a feature be most easily implemented?
1 If you think variable variables are always bad, feel free to state your case but the main question is: how can they be implemented?
This is a feature deeply embedded in dynamic languages. C# has its roots as a static, object-oriented language, and up to C# 3.0 this means no luck accomplishing what you want in any proper way. However, C# 4.0/.NET 4.0 introduces the
dynamickeyword, which allows variables to be dynamically typed, as in PHP. Unfortunately, although this is a leap forward in the path of C# becoming a static/dynamic hybrid language, it is missing the crucialevalfunction that almost every dynamic language has. With the rumoured compiler-as-a-service feature of C# 5.0/.NET 5.0, this will effectively be introduced (though the internal behaviour would not be the same). Until then, there’s no decent solution short of the hack of using aDictionaryto store variable names.