title: C# okay with comparing value types to null
body: <p>I ran into this today and have no idea why the C# compiler isn't throwing an error.</p>
  
  <pre><code>Int32 x = 1;
  if (x == null)
  {
      Console.WriteLine("What the?");
  }
  </code></pre>
  
  <p>I'm confused as to how x could ever possibly be null. Especially since this assignment definitely throws a compiler error:</p>
  
  <pre><code>Int32 x = null;
  </code></pre>
  
  <p>Is it possible that x could become null, did Microsoft just decide to not put this check into the compiler, or was it missed completely?</p>
  
  <p>Update: After messing with the code to write this article, suddenly the compiler came up with a warning that the expression would never be true. Now I'm really lost. I put the object into a class and now the warning has gone away but left with the question, can a value type end up being null.</p>
  
  <pre><code>public class Test
  {
      public DateTime ADate = DateTime.Now;
  
      public Test ()
      {
          Test test = new Test();
          if (test.ADate == null)
          {
              Console.WriteLine("What the?");
          }
      }
  }
  </code></pre>