title: C# - Calling a struct constructor that has all defaulted parameters
body: <p>I ran into this issue today when creating a <code>struct</code> to hold a bunch of data. Here is an example:</p>
  
  <pre><code>public struct ExampleStruct
  {
      public int Value { get; private set; }
  
      public ExampleStruct(int value = 1)
          : this()
      {
          Value = value;
      }
  }
  </code></pre>
  
  <p>Looks fine and dandy. The problem is when I try to use this constructor without specifying a value and desiring to use the defaulted value of 1 for the parameter:</p>
  
  <pre><code>private static void Main(string[] args)
  {
      ExampleStruct example1 = new ExampleStruct();
  
      Console.WriteLine(example1.Value);
  }
  </code></pre>
  
  <p>This code outputs <code>0</code> and does not output <code>1</code>. The reason is that all structs have public parameter-less constructors. So, like how I'm calling <code>this()</code> my explicit constructor, in <code>Main</code>, that same thing occurs where <code>new ExampleStruct()</code> is actually calling <code>ExampleStruct()</code> but not calling <code>ExampleStruct(int value = 1)</code>. Since it does that, it uses <code>int</code>'s default value of 0 as <code>Value</code>.</p>
  
  <p>To make matters worse, my actual code is checking to see that <code>int value = 1</code> parameter is within a valid range within the constructor. Add this to the <code>ExampleStruct(int value = 1)</code> constructor above:</p>
  
  <pre><code>if(value &lt; 1 || value &gt; 3)
  {
      throw new ArgumentException("Value is out of range");
  }
  </code></pre>
  
  <p>So, as it stands right now, the default constructor actually created an object that is invalid in the context I need it for. Anyone know how I can either:</p>
  
  <ul>
  <li>A. Call the <code>ExampleStruct(int value = 1)</code> constructor.</li>
  <li>B. Modify how the default values are populated for the <code>ExampleStruct()</code> constructor.</li>
  <li>C. Some other suggestion/option.</li>
  </ul>
  
  <p>Also, I am aware that I could use a field like this instead of my <code>Value</code> property:</p>
  
  <pre><code>public readonly int Value;
  </code></pre>
  
  <p>But my philosophy is to use fields privately unless they are <code>const</code> or <code>static</code>.</p>
  
  <p>Lastly, the reason I'm using a <code>struct</code> instead of a <code>class</code> is because this is simply an object to hold non-mutable data, should be fully populated when it is constructed, and when passed as a parameter, should not be able to be <code>null</code> (since it is passed by value as a <code>struct</code>), which is what struct's are designed for.</p>