References to variables in C#?

A read-only array reference:

class ArrayRef<T>
{
   private T[] array;
   private int index;

   public ArrayRef(T[] array, int index)
   {
      this.array = array;
      this.index = index;
   }

   public static implicit operator T(ArrayRef self)
   {
      return self.array[self.index];
   }
}

var s = new string[] { "one", "two", "three", "four", "five", "six" };
var sr = new ArrayRef<string>[] { new ArrayRef<string>(s, 0), new ArrayRef<string>(s, 1), new ArrayRef<string>(s, 2), new ArrayRef<string>(s, 3), new ArrayRef<string>(s, 4), new ArrayRef<string>(s, 5) };

Console.WriteLine(sr[1]); // == "two"
s[1] = "two point zero";
Console.WriteLine(sr[1]); // == "two point zero"

In managed code references are used instead of pointers, as the garbage collector can move objects around in memory at any moment.

To have a reference to something it has to be an object, so you can't have references to the individual items in an integer array. As strings are objects, you can have references to the individual strings by just copying the references in the array:

string[] s = new string[] { "one", "two", "three", "four", "five", "six" };
string[] sr = new string[] { s[0], s[1], s[2], s[3], s[4], s[5] };

However, as strings are immutable objects you can only use the references to read the items. If you assign a string to a reference in the sr array, you will overwrite the reference instead of changing the object that it points to.

If you want to change the objects, you will have to have mutable objects. For example:

StringBuilder[] s = new StringBuilder[] {
   new StringBuilder("one"),
   new StringBuilder("two"),
   new StringBuilder("three"),
};
StringBuilder[] sr = new StringBuilder[] { s[0], s[1], s[2] };

Console.WriteLine(s[1]); // == "two"
sr[1].Append(" point zero");
Console.WriteLine(s[1]); // == "two point zero"

Since C#7 it is possible to define a ref local to an element in an array or a field in an object:

string[] s = new string[] { "one", "two", "three", "four", "five", "six" };
ref string sr1 = ref s[1];  // a ref local to an element in an array
Console.WriteLine(sr1); // == "two"
sr1 = "two point zero";
Console.WriteLine(s[1]); // == "two point zero"

No. Putting unsafe code aside, which does allow holding pointers to memory locations, there's no way to store a reference to a variable in C#.

ref and out arguments provide the only means to take a reference but you can't save them anywhere.

You can workaround this limitation by wrapping fields in a class and using its reference instead. This is what the compiler does to capture variables in closures:

For instance, when you write:

int integer = 0;
Action<int> method = i => Console.WriteLine(i + integer);
integer = 42;
method(100); // prints 142, not 100

In the second line, the compiler will have to take out the anonymous method and store it as a separate method in the class. Obviously, that method won't have access to integer variable. It somehow needs to pass a "reference" to integer variable to that anonymous method. Since it's not possible, it'll generate a class with a field to hold an integer and uses an instance of that class to store the variable. Basically, the local variable is promoted to a field in a class and is stored in the heap.

Tags:

C#