Comparing 'int' to 'null' compiles
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/1972262/c-sharp-okay-with-comparing-value-types-to-null">C# okay with comparing value types to null</a>  </p>
</blockquote>



<p>I came a cross something I find strange in the C# (4.0) compiler just now.</p>

<pre><code>int x = 0;
if (x == null) // Only gives a warning - 'expression is always false'
    x = 1;

int y = (int)null; // Compile error
int z = (int)(int?)null; // Compiles, but runtime error 'Nullable object must have a value.'
</code></pre>

<p>If you cannot assign <code>null</code> to an <code>int</code>, why does the compiler allow you to compare them (It gives a warning only)?</p>

<p>Interestingly, the compiler does not allow the following:</p>

<pre><code>struct myStruct
{
};

myStruct s = new myStruct();
if (s == null) // does NOT compile
    ;
</code></pre>

<p>Why does the <code>struct</code> example not compile, but the <code>int</code> example does?</p>

<p>Thanks for your help,</p>

<p>Richard Hughes</p>
