title: How do I assign by "reference" to a class field in C#?
body: <p>I am trying to understand how to assign by &quot;reference&quot; to a class field in C#.</p>
  <p>I have the following example to consider:</p>
  <pre><code> public class X
   {
  
       public X()
       {
           string example = &quot;X&quot;;
  
           new Y(ref example);
  
           new Z(ref example);
  
           System.Diagnostics.Debug.WriteLine(example);
       }
  
   }
  
   public class Y
   {
  
       public Y( ref string example )
       {
           example += &quot; (Updated By Y)&quot;;
       }
  
   }
  
   public class Z
   {
       private string _Example;
  
       public Z(ref string example)
       {
  
           this._Example = example;
  
           this._Example += &quot; (Updated By Z)&quot;;
       }
   }
  
   var x = new X();
  </code></pre>
  <p>When running the above code the output is:</p>
  <blockquote>
  <p>X (Updated By Y)</p>
  </blockquote>
  <p>And not:</p>
  <blockquote>
  <p>X (Updated By Y) (Updated By Z)</p>
  </blockquote>
  <p>As I had hoped.</p>
  <p>It seems that assigning a &quot;ref parameter&quot; to a field loses the reference.</p>
  <p>Is there a way to keep hold of the reference when assigning to a field?</p>