title: .NET Parameter passing - by reference v/s by value
body: <p>I'm trying to validate my understanding of how C#/.NET/CLR treats value types and reference types. I've read so many contradicting explanations I stil</p>
  
  <p>This is what I understand today, please correct me if my assumptions are wrong.</p>
  
  <p>Value types such as int etc live on the stack, Reference types live on the managed heap <strong>however</strong> if a reference type has for example has an instance variable of type double, it will live along with its object on the heap </p>
  
  <p>The second part is what I am most confused about.</p>
  
  <p>Lets consider a simple class called Person.</p>
  
  <p>Person has a property called Name.</p>
  
  <p>Lets say I create an instance of Person in another class, we'll call it UselessUtilityClass.</p>
  
  <p>Consider the following code:</p>
  
  <pre><code>class UselessUtilityClass
  {
     void AppendWithUnderScore(Person p)
     {
       p.Name = p.Name + "_";
     }
  }
  </code></pre>
  
  <p>and then somewhere we do:</p>
  
  <pre><code>Person p = new Person();
  p.Name = "Priest";
  UselessUtilityClass u = new UselessUtilityClass();
  u.AppendWithUnderScore(p);
  </code></pre>
  
  <p>Person is a reference type, when passed to UselessUtilityClass -- this is where I go - nuts...the <strong>VARIABLE</strong> p which is an instance of the Person reference is passed by <strong>VALUE</strong>, which means when I write p.Name I will see "Priest_"</p>
  
  <p>And then if I wrote</p>
  
  <pre><code>Person p2 = p;
  </code></pre>
  
  <p>And I do </p>
  
  <p>p2.Name = "Not a Priest";</p>
  
  <p>And write p's name like below I will get "Not a Priest"</p>
  
  <pre><code>Console.WriteLine(p.Name) // will print "Not a Priest"
  </code></pre>
  
  <p>This is because they are reference types and point to the same address in memory.</p>
  
  <p>Is my understanding correct?</p>
  
  <p>I think there is some misunderstanding going on when people say <strong>All objects in .NET are passed by Reference</strong>, this doesn't jive based on what I think. I could be wrong, thats why I have come to the Stackers.</p>