title: Why do I have to typecast an int in a ternary expression?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2215745/conditional-operator-cannot-cast-implicitly">Conditional operator cannot cast implicitly?</a>  </p>
  </blockquote>
  
  
  
  <p>I have run into a peculiar situation and want to know why I have to do it. I'm using <a href="http://en.wikipedia.org/wiki/.NET_Framework" rel="nofollow noreferrer">.NET</a> 3.5.</p>
  
  <p>This works:</p>
  
  <pre><code>short foo;
  
  if (isValid)
      foo = -1;
  else
      foo = getFoo();
  </code></pre>
  
  <p>This does not work:</p>
  
  <pre><code>short foo;
  foo = isValid ? -1 : getFoo();
  </code></pre>
  
  <p>I have to typecast -1:</p>
  
  <pre><code>short foo;
  foo = isValid ? (short)-1 : getFoo();
  </code></pre>
  
  <p>What does the ternary expression do differently? It considers the -1 to be an int that needs to be cast into a short. But why?</p>