1

Is it possible to get the name of a variable that points to a reference? Take the example below:

private void FillArray(int[] array, int count)
{
    array = new int[count];

    for (int i = 0; i < array.Length; i++)
    {
        array[i] = i;
    }

    Debug.WriteLine("Array {0} is now filled up with {1} values", array.Name, count);
}

There is no property Name for the array that is going to be used in the above method. I know that a workaround would be adding another parameter as string and assign the name to it, but how to do this programitically? Also I know I can make my own implementation, but I am interested to know the answer to this particular question!

Dumbo
  • 13,555
  • 54
  • 184
  • 288
  • Are you after the variable name in the caller to this method? – Reed Copsey Jun 09 '12 at 21:40
  • yes! if I pass an int[] field named 'myArray` I want to know the name inside the method to write on the debug output. – Dumbo Jun 09 '12 at 21:48
  • 1
    That's not possible. Local variable names are only a language construct, and really don't exist at runtime. – Reed Copsey Jun 09 '12 at 21:49
  • Probably the closest thing would be the `[CallerMemberName]` in C# 5, but that's only going to give you the method or property name of the caller, and is "filled in" by the compiler, not provided by the runtime. – Reed Copsey Jun 09 '12 at 21:51

4 Answers4

3

Is it possible to get the name of a variable that points to a reference?

No - there could be multiple variables pointing to that same reference. The variable name is unrelated to the reference itself.

Note that you can use expressions to get the name of a property (which could be a property with a backing field of a reference type), using a technique like the one in this answer. In some scenarios, this can avoid requiring you to provide a string, and ease refactoring, but is limited (as it will only work via properties, etc).

Community
  • 1
  • 1
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
2

Is it possible to get the name of a variable that points to a reference?

No.

In general there could be multiple variables with different names that all hold references to the same object. There is no list of these names stored in the runtime.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
2

(Local) Variable names are just names in the language that are given to a storage location. After compilation those names do not exist anymore. So in general it's impossible to get any variable names at runtime.

This is different for fields, properties, methods and other stuff that can be queried about classes, of course.

Joey
  • 344,408
  • 85
  • 689
  • 683
1

No, the reference has no name.

It's not the variable that is sent to the method, it's the reference that was the value of the variable.

If there even was a variable, that is. The method could just as well be called with a newly created array, so there would be no variable at all:

FillArray(new int[4], 4)

Also, your method will ditch the array that is sent into it and create a new array, so you can just as well call it without any array at all:

FillArray(null, 4)

Your method doesn't change the variable to reference the new array, it just throws away the new array. You would need to use the ref or out keyword for that:

private void FillArray(out int[] array, int count) {
  array = new int[count];
  for (int i = 0; i < array.Length; i++) {
    array[i] = i;
  }
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Yeah I figured out that it didnt do anything with `int[]` fields I declared in the class. – Dumbo Jun 09 '12 at 21:51