Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I assign by "reference" to a class field in c#?

I am trying to understand how to assign by "reference" to a class field in C#.

I have the following example to consider:

 public class X  {       public X()      {          string example = "X";           new Y(ref example);           new Z(ref example);           System.Diagnostics.Debug.WriteLine(example);      }   }   public class Y  {       public Y( ref string example )      {          example += " (Updated By Y)";      }   }   public class Z  {      private string _Example;       public Z(ref string example)      {           this._Example = example;           this._Example += " (Updated By Z)";      }  }   var x = new X(); 

When running the above code the output is:

X (Updated By Y)

And not:

X (Updated By Y) (Updated By Z)

As I had hoped.

It seems that assigning a "ref parameter" to a field loses the reference.

Is there a way to keep hold of the reference when assigning to a field?

like image 351
Jamie Avatar asked Jun 05 '10 13:06

Jamie


People also ask

How do you pass reference parameters in C#?

Passing by reference enables function members, methods, properties, indexers, operators, and constructors to change the value of the parameters and have that change persist in the calling environment. To pass a parameter by reference with the intent of changing the value, use the ref , or out keyword.

Are class objects passed by reference in C#?

By default, C# does not allow you to choose whether to pass each argument by value or by reference. Value types are passed by value. Objects are not passed to methods; rather, references to objects are passed—the references themselves are passed by value.

Can a reference variable be reinitialized?

I have read about C++ reference type variables as they cannot be reinitialized or reassigned, since they are stored 'internally' as constant pointers. So a compiler would give a error.

How do you reference a class in C++?

A reference to a class is declared using the '&' symbol. When declaring a variable, which is a reference, you must immediately initialize it with the value of the object for which you already allocated memory.


1 Answers

As others have noted, you cannot have a field of "ref to variable" type. However, just knowing that you cannot do it is probably unsatisfying; you probably also want to know first, why not, and second, how to get around this restriction.

The reason why is because there are only three possibilities:

1) Disallow fields of ref type

2) Allow unsafe fields of ref type

3) Do not use the temporary storage pool for local variables (aka "the stack")

Suppose we allowed fields of ref type. Then you could do

public ref int x; void M() {     int y = 123;     this.x = ref y; } 

and now y can be accessed after M completes. This means that either we're in case (2) -- accessing this.x will crash and die horribly because the storage for y no longer exists -- or we're in case (3), and the local y is stored on the garbage collected heap, not the temporary memory pool.

We like the optimization that local variables be stored on the temporary pool even if they are being passed by ref, and we hate the idea that you could leave a time bomb around that could make your program crash and die later. Therefore, option one it is: no ref fields.

Note that for local variables that are closed-over variables of anonymous functions we choose option (3); those local variables are not allocated out of the temporary pool.

Which then brings us to the second question: how do you get around it? If the reason you want a ref field is to make a getter and setter of another variable, that's perfectly legal:

sealed class 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); } } } ... Ref<int> x; void M() {     int y = 123;     x = new Ref<int>(()=>y, z=>{y=z;});     x.Value = 456;     Console.WriteLine(y); // 456 -- setting x.Value changes y. } 

And there you go. y is stored on the gc heap, and x is an object that has the ability to get and set y.

Note that the CLR does support ref locals and ref returning methods, though C# does not. Perhaps a hypothetical future version of C# will support these features; I have prototyped it and it works well. However, this is not real high on the priority list, so I wouldn't get my hopes up.

UPDATE: The feature mentioned in the paragraph above was finally implemented for real in C# 7. However, you still cannot store a ref in a field.

like image 95
Eric Lippert Avatar answered Oct 12 '22 15:10

Eric Lippert