title: C# casting to nullable type?
body: <p><strong>Beyond</strong> the regular boring difference between <code>Cast</code> and <code>As</code></p>
  
  <ul>
  <li>if i <em>know</em> that <em>apple</em> is a <em>Fruit</em> so I can use <code>(Fruit)apple</code> - and it throws an exception if it aint </li>
  <li><code>as value</code> can be checked against null to see if succeeded [won't throw Exception...]</li>
  </ul>
  
  <p>However Ive been reading @EricLippert <a href="http://blogs.msdn.com/b/ericlippert/archive/2009/10/08/what-s-the-difference-between-as-and-cast-operators.aspx">article</a> about this and there was a nice sample about Nullable Value Types : </p>
  
  <pre><code>short? s = (short?)123;
  int? i = s as int?;
  </code></pre>
  
  <p>this <em>won't</em> compile... </p>
  
  <p><sub>Cannot convert type 'short?' to 'int?' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion</sub></p>
  
  <p><strong>Fine.</strong></p>
  
  <p>so why this : </p>
  
  <pre><code>    short? s = (short?)123;
      int? i = (int?)s;
  </code></pre>
  
  <p><strong>Does</strong> Compile ? ( <em>Against ALL Expectations</em> ! I <strong>KNOW</strong> that <code>s</code> is not  <code>int?</code>  - and it should go BANG but it aint ...)</p>
  
  <p>the Cast checking here should be much more deadly than the former example (which went Bang)</p>
  
  <p>I feel bad asking about this much-talked subject.</p>
  
  <p>Thanks in Advance.</p>