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!
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:
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:
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
reforoutkeyword for that: