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!

4
  • Are you after the variable name in the caller to this method? Commented Jun 9, 2012 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. Commented Jun 9, 2012 at 21:48
  • 1
    That's not possible. Local variable names are only a language construct, and really don't exist at runtime. Commented Jun 9, 2012 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. Commented Jun 9, 2012 at 21:51

4 Answers 4

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).

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

Comments

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.

Comments

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.

Comments

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;
  }
}

1 Comment

Yeah I figured out that it didnt do anything with int[] fields I declared in the class.

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.