title: Are value types immutable by definition?
body: <p>I frequently read that <code>struct</code>s should be immutable - aren't they by definition?</p>
  
  <p>Do you consider <code>int</code> to be immutable?</p>
  
  <pre><code>int i = 0;
  i = i + 123;
  </code></pre>
  
  <p>Seems okay - we get a new <code>int</code> and assign it back to <code>i</code>. What about this?</p>
  
  <pre><code>i++;
  </code></pre>
  
  <p>Okay, we can think of it as a shortcut.</p>
  
  <pre><code>i = i + 1;
  </code></pre>
  
  <p>What about the <code>struct</code> <code>Point</code>?</p>
  
  <pre><code>Point p = new Point(1, 2);
  p.Offset(3, 4);
  </code></pre>
  
  <p>Does this really mutate the point <code>(1, 2)</code>? Shouldn't we think of it as a shortcut for the following with <code>Point.Offset()</code> returning a new point?</p>
  
  <pre><code>p = p.Offset(3, 4);
  </code></pre>
  
  <p>The background of this thought is this - how can a value type with no identity be mutable? You have to look at it at least twice to determine if it changed. But how can you do this without an identity?</p>
  
  <p>I don't want to complicate reasoning about this by considering <code>ref</code> parameters and boxing. I am also aware that <code>p = p.Offset(3, 4);</code> expresses immutability much better than <code>p.Offset(3, 4);</code> does. But the question remains - aren't value types immutable by definition?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I think there are at least two concepts involved - the mutability of a variable or field and the mutability of the value of a variable.</p>
  
  <pre><code>public class Foo
  {
      private Point point;
      private readonly Point readOnlyPoint;
  
      public Foo()
      {
          this.point = new Point(1, 2);
          this.readOnlyPoint = new Point(1, 2);
      }
  
      public void Bar()
      {
          this.point = new Point(1, 2);
          this.readOnlyPoint = new Point(1, 2); // Does not compile.
  
          this.point.Offset(3, 4); // Is now (4, 6).
          this.readOnlyPoint.Offset(3, 4); // Is still (1, 2).
      }
  }
  </code></pre>
  
  <p>In the example we have to fields - a mutable one and a immutable one. Because a value type field contains the whole value, a value type stored in a immutable field must be immutable, too. I am still quite suprised by the result - I did not exspect the readonly field to remain unmodified.</p>
  
  <p>Variables (besides constants) are allways mutable, hence they imply no restriction on the mutability of value types.</p>
  
  <hr>
  
  <p>The answer seems not to be that straight forward so I will rephrase the question.</p>
  
  <p>Given the following.</p>
  
  <pre><code>public struct Foo
  {
      public void DoStuff(whatEverArgumentsYouLike)
      {
          // Do what ever you like to do.
      }
  
      // Put in everything you like - fields, constants, methods, properties ...
  }
  </code></pre>
  
  <p>Can you give a completed version of <code>Foo</code> and an usage example - that may include <code>ref</code> parameters and boxing - so that it is not possible to rewrite all occurences of</p>
  
  <pre><code>foo.DoStuff(whatEverArgumentsYouLike);
  </code></pre>
  
  <p>with</p>
  
  <pre><code>foo = foo.DoStuff(whatEverArgumentsYouLike);
  </code></pre>