-3

If I have a ton of int arrays for example in a method and I input a string is there a way of finding the variable name that corresponds to that string and then returning that array?

4
  • I don't understand, can you improve the explanation? Maybe a example can help? Commented Jul 22, 2021 at 13:42
  • Maybe put your arrays in a dictionary? Commented Jul 22, 2021 at 13:43
  • Show us the code, please. Commented Jul 22, 2021 at 13:45
  • What did you try so far? Add a clear example; otherwise, it will be hard to help you. How is your problem different from using a Dictionary<string, int[]>, which would be an obvious solution for accessing arrays by name? Commented Jul 22, 2021 at 13:50

2 Answers 2

0

You can have a Dictionary<string, int[]>.

Where the key (string) will be the name for each array (value).

Then you can search by the dictionary key, e.g:

string input = <Your Input>;
IDictionary<string, int[]> arraysDic = 
   <Initialize with the arrays you have and a name for each array>;

if (arraysDic.ContainsKey(input))
{
    return arraysDic[input];
}
Sign up to request clarification or add additional context in comments.

Comments

0

No, there's no built-in way to access a local variable just by its name. In fact, local variable names are often changed by the compiler or inlined away, so it would not be stable.

I would recommend using a different data structure than "a ton of int arrays" if you need to access them dynamically. A dictionary seems like a reasonable choice, or a class or struct to hold the arrays.

Another option if you have variables like array1, array2, array3... would be to use a List of arrays and access them by index instead of by name.

1 Comment

To support your first paragraph, the local variable names aren't even part of the compiled assemblies (.dll/.exe); they're encoded as part of the debugging information (.pdb).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.