0

Say I have a List called this_list. If I make a string that says "this_list", how can I turn that string into the actual variable? It should do the opposite of what nameof would do.

List<string> this_list = new List<string> { "wow","amazing" };
string str = "this_list";
// str to this_list somehow
4
  • 1
    Why would you want to do that? I can't see how it's useful and I see no way of accomplishing it. Commented Jan 25, 2022 at 9:33
  • Have a look here: stackoverflow.com/questions/8723960/… and here: stackoverflow.com/questions/8831273/… Commented Jan 25, 2022 at 9:34
  • @Palle Due I have a load of string lists and I need to get only a certain one based off a string. Commented Jan 25, 2022 at 9:40
  • 1
    Then why not use a Dictionary<string, List<string>>? Commented Jan 25, 2022 at 9:41

1 Answer 1

0

If this_list is a local variable (like in your example), you can't. Local variable names are lost during compilation.

If this_list is a field or property, you can use Reflection.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.