title: Automatic Properties and Structures Don't Mix?
body: <p>Kicking around some small structures while answering <a href="https://stackoverflow.com/questions/414981/directly-modifying-listt-elements">this post</a>, I came across the following unexpectedly:</p>
  
  <p>The following structure, using an int field is perfectly legal:</p>
  
  <pre><code>struct MyStruct
  { 
      public MyStruct ( int size ) 
      { 
          this.Size = size; // &lt;-- Legal assignment.
      } 
  
      public int Size; 
  }
  </code></pre>
  
  <p>However, the following structure, using an automatic property does not compile:</p>
  
  <pre><code>struct MyStruct
  { 
      public MyStruct ( int size ) 
      { 
          this.Size = size; // &lt;-- Compile-Time Error!
      } 
  
      public int Size{get; set;}
  }
  </code></pre>
  
  <p>The error returned is  "The 'this' object cannot be used before all of its fields are assigned to". I know that this is standard procedure for a struct: the backing field for any property must be assigned directly (and not via the property's set accessor) from within the struct's constructor. </p>
  
  <p>A solution is to use an explicit backing field:</p>
  
  <pre><code>struct MyStruct
  { 
      public MyStruct(int size)
      {
          _size = size;
      }
  
      private int _size;
  
      public int Size
      {
          get { return _size; }
          set { _size = value; }
      }
  }
  </code></pre>
  
  <p>(Note that VB.NET would not have this issue, because in VB.NET all fields are automatically initialized to 0/null/false when first created.)</p>
  
  <p>This would seem to be an unfortunate limitation when using automatic properties with structs in C#. Thinking conceptually, I was wondering if this wouldn't be a reasonable place for there to be an exception that allows the property set accessor to be called within a struct's constructor, at least for an automatic property?</p>
  
  <p>This is a minor issue, almost an edge-case, but I was wondering what others thought about this...</p>