Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're not changing anything about the object a1 points to, but instead changing which object a1 points to.</p> <p><a href="http://dev.morethannothing.co.uk/valuevsreference/Valuevs.Reference3.png" rel="noreferrer">a = new Person(); b = a; b = new Person(); http://dev.morethannothing.co.uk/valuevsreference/Valuevs.Reference3.png</a></p> <p>Your example replaces "new Person { … }" with a string literal, but the principle is the same.</p> <p>The difference comes when you're changing properties of the object. Change the property of a value type, and it's not reflected in the original.</p> <p><a href="http://dev.morethannothing.co.uk/valuevsreference/Valuevs.Reference1.png" rel="noreferrer">a = new Person(); b = a; b.Name = …; http://dev.morethannothing.co.uk/valuevsreference/Valuevs.Reference1.png</a></p> <p>Change the property of a reference type, and it is reflected in the original.</p> <p><a href="http://dev.morethannothing.co.uk/valuevsreference/Valuevs.Reference2.png" rel="noreferrer">a = new Person(); b = a; b.Name = …; http://dev.morethannothing.co.uk/valuevsreference/Valuevs.Reference2.png</a></p> <p>p.s. Sorry about the size of the images, they're just from something I had lying around. You can see the full set at <a href="http://dev.morethannothing.co.uk/valuevsreference/" rel="noreferrer">http://dev.morethannothing.co.uk/valuevsreference/</a>, which covers value types, reference types, and passing value types by value and by reference, and passing reference types by value and by reference.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload