Is it Possible to Return a Reference to a Variable in C#?

Betamoo picture Betamoo · Dec 28, 2010 · Viewed 31.3k times · Source

Can I return a reference to a double value for example?

This is what I want to do:

ref double GetElement()
{
   ......
   // Calculate x,y,z
   return ref doubleArray[x,y,z];
}

To use it like this

void func()
{
   GetElement()=5.0;
}

It is like returning a double pointer in C++ ... I know that the way I wrote it is wrong.. but is there a correct way for doing it?

Answer

Eric Lippert picture Eric Lippert · Dec 28, 2010

UPDATE: The desired feature is now supported in C# 7.


The CLR type system does support ref-returning methods, and I have written an experimental prototype of the C# compiler which supports the feature you want. (The prototype also implements ref-typed local variables, but ref-typed fields are illegal in the CLR type system.)

You have hit upon exactly the syntax I chose for the prototype, which means that either great minds think alike, or that fools never differ.

Though the prototype works quite nicely, it is very unlikely that this will make the bar to become a feature of the next version of the C# language. Very few customers want this feature, it is fairly expensive to implement, we have a list as long as your arm of more important features, and there are other ways to make this sort of thing work without adding this complexity to the type system. These are all huge "points against" doing the feature.

For example, you could make a pair of delegates:

struct Ref<T>
{
    private readonly Func<T> getter;
    private readonly Action<T> setter;
    public Ref(Func<T> getter, Action<T> setter)
    {
        this.getter = getter;
        this.setter = setter;
    }
    public T Value { get { return getter(); } set { setter(value); } }
}

var arr = new int[10];
var myref = new Ref<int>(()=>arr[1], x=>arr[1]=x);
myref.Value = 10;
Console.WriteLine(myref.Value);

That is considerably slower than the same feature implemented with ref returns, but the benefit is that you can make a Ref<T> in places where ref is not legal. For example, you can store Ref<T> in a field, which you cannot do with a ref-returning method.

If you have a really awesome compelling scenario for why you need ref-returning methods, I would love to hear about it. The more real-world scenarios we have, the more likely it is that such a feature might get implemented in a hypothetical future version of the language.

See also related questions:

Can I use a reference inside a C# function like C++?

Why doesn't C# support the return of references?

and my blog post on the subject:

http://ericlippert.com/2011/06/23/ref-returns-and-ref-locals/