Search code examples
c#variablesvariable-assignment

Access value of variable whose name is stored in another variable


Using C#, is it possible to access the content of a variable whose name is stored in another string variable?

eg.

string str ="ABCDEFG";
string variable = "str";

How could I access the value of str using variable?


Solution

  • you probably can, but that's too complicated. Have you thought of using the Dictionary class?

    Dictionary<string,string> myDictionary = new Dictionary<string,string>();
    
    myDictionary["str"] = "ABCDEF";
    
    var valueinstr = myDictionary["str"];