title: Can structs really not be null in C#?
body: <p>Below is some code that demonstrates I cannot declare and initialize a struct type as null.  The <a href="http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx" rel="noreferrer">Nullable</a> type is a struct, so why am I able to set it to null?</p>
  
  <pre><code>Nullable&lt;bool&gt; b = null;
  if (b.HasValue)
  {
      Console.WriteLine("HasValue == true");
  }
  
  //Does not compile...
  Foo f = null;
  if (f.HasValue)
  {
      Console.WriteLine("HasValue == true");
  }
  </code></pre>
  
  <p>Where <code>Foo</code> is defined as</p>
  
  <pre><code>public struct Foo
  {
      private bool _hasValue;
      private string _value;
  
      public Foo(string value)
      {
          _hasValue = true;
          _value = value;
      }
  
      public bool HasValue
      {
          get { return _hasValue; }
      }
  
      public string Value
      {
          get { return _value; }
      }
  }
  </code></pre>
  
  <p>The question has been answered (see below).  To clarify I'll post an example.  The C# code:</p>
  
  <pre><code>using System;
  
  class Program
  {
      static void Main(string[] args)
      {
          Nullable&lt;bool&gt; a;
          Nullable&lt;bool&gt; b = null;
      }
  }
  </code></pre>
  
  <p>produces the following IL:</p>
  
  <pre><code>.method private hidebysig static void  Main(string[] args) cil managed
  {
    .entrypoint
    // Code size       10 (0xa)
    .maxstack  1
    .locals init ([0] valuetype [mscorlib]System.Nullable`1&lt;bool&gt; a,
             [1] valuetype [mscorlib]System.Nullable`1&lt;bool&gt; b)
    IL_0000:  nop
    IL_0001:  ldloca.s   b
    IL_0003:  initobj    valuetype [mscorlib]System.Nullable`1&lt;bool&gt;
    IL_0009:  ret
  } // end of method Program::Main
  </code></pre>
  
  <p>a and b are declared, but only b is initialized.</p>