title: Generics: casting and value types, why is this illegal?
body: <p>Why is this a compile time error?</p>
  <pre><code>public TCastTo CastMe&lt;TSource, TCastTo&gt;(TSource i)
  {
       return (TCastTo)i;
  }
  </code></pre>
  <p>Error:</p>
  <blockquote>
  <p>Cannot convert type 'TSource' to 'TCastTo'</p>
  </blockquote>
  <p>And why is this a runtime error?</p>
  <pre><code>public TCastTo CastMe&lt;TSource, TCastTo&gt;(TSource i)
  {
       return (TCastTo)(object)i;
  }
  
  int a = 4;
  long b = CastMe&lt;int, long&gt;(a); // InvalidCastException
  
  // this contrived example works
  int aa = 4;
  int bb = CastMe&lt;int, int&gt;(aa);
  
  // this also works, the problem is limited to value types
  string s = &quot;foo&quot;;
  object o = CastMe&lt;string, object&gt;(s);
  </code></pre>
  <p>I've searched SO and the internet for an answer to this and found lots of explanations on similar generic related casting issues, but I can't find anything on this particular simple case.</p>