title: Ternary operator VB vs C#: why resolves Nothing to zero?
body: <p>I just shoot myself in the foot and would like to know whether there were actual reasons to make this situation possible.<br>
  And anyway, this question can stay for the convenience of the future foot shooters.</p>
  
  <hr>
  
  <p>Suppose we have a nullable value in vb.net:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Dim i as Integer?
  </code></pre>
  
  <p>We want to assign a value to it, basing on a condition, and using a ternary operator, because it's so neat and stuff:</p>
  
  <pre class="lang-vb prettyprint-override"><code>i = If(condition(), Nothing, 42)
  </code></pre>
  
  <p>That is, if a condition is <code>true</code>, employ the nullability, otherwise the value.<br>
  At which point the shooting occurs. For no apparent reason VB compiler decides that the common base type for <code>Nothing</code> and <code>Integer</code> is <code>Integer</code>, at which point it silently translates the statement to:</p>
  
  <pre class="lang-vb prettyprint-override"><code>i = If(condition(), 0, 42)
  </code></pre>
  
  <hr>
  
  <p>Now, if you were to do this in C#:</p>
  
  <pre class="lang-cs prettyprint-override"><code>i = (condition()) ? null : 42;
  </code></pre>
  
  <p>You would immediately get a compiler error saying that <code>&lt;null&gt;</code> doesn't mix well with <code>int</code>. Which is great, as my foot would have been healthier had I went the C# way this time. And for this to compile, you have to explicitly write:</p>
  
  <pre class="lang-cs prettyprint-override"><code>i = (condition()) ? null : (int?)42;
  </code></pre>
  
  <hr>
  
  <p>Now, you <em>can</em> do the same in VB and get the correct null-ness you would expect:</p>
  
  <pre class="lang-vb prettyprint-override"><code>i = If(condition(), Nothing, CType(42, Integer?))
  </code></pre>
  
  <p>But that requires having your foot shot in the first place. There's no compiler error and there's no warning. That's with <code>Explicit On</code> and <code>Strict On</code>.</p>
  
  <hr>
  
  <p>So my question is, why?<br>
  Should I take this as a compiler bug?<br>
  Or can someone explain why the compiler behaves this way?</p>