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?
2 Answers
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];
}
Comments
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
.dll/.exe); they're encoded as part of the debugging information (.pdb).
Dictionary<string, int[]>, which would be an obvious solution for accessing arrays by name?