title: FirstOrDefault Behavior with Int and Int?
body: <p>I just read a SO <a href="https://stackoverflow.com/questions/1825304/return-null-for-firstordefault-on-empty-ienumerableint.">post</a> that explained that <code>FirstOrDefault()</code>'s return type will vary based on the value of the element being selected.</p>
  
  <p>Example:</p>
  
  <pre><code>ICollection&lt;String&gt; list = new List&lt;String&gt;();
  list.Add("bye");
  
  int a = (from x in list where (x == "hi") select x.Length).FirstOrDefault(); 
  </code></pre>
  
  <p>In this example, <code>a</code> will be equal to 0 since the <code>int</code> default value is 0.</p>
  
  <p>However, I can append <code>.Cast&lt;int?&gt;()</code> as per the already linked post in order to get <code>null</code> when the query returns 0 results.</p>
  
  <pre><code>int? a = (from x in list where ... x.Length).Cast&lt;int?&gt;().FirstOrDefault();
  </code></pre>
  
  <p>Why don't I get a compile-time error (or at least a warning) when, for my first example, I use a Nullable int (<code>int?</code>) rather than a regular <code>int</code>? </p>
  
  <p>If I understand correctly, using a <code>int?</code> when performing my first query will <strong>never</strong> result in a value of <code>null</code>.</p>