title: IEnumerable<T> Covariance With ints/longs
body: <p>I have the following code:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          int a = 1;
          long b = a; //This works at runtime.
  
          IEnumerable&lt;int&gt; ints = new List&lt;int&gt; { 0, 1, 2, 3 };
          IEnumerable&lt;long&gt; longs = ints as IEnumerable&lt;long&gt;; //This returns null at runtime.
  
          Child child = new Child();
          Parent parent = child; //This works at runtime.
  
          IEnumerable&lt;Child&gt; children = new List&lt;Child&gt; { new Child() };
          IEnumerable&lt;Parent&gt; parents = children as IEnumerable&lt;Parent&gt;; //This returns non-null at runtime.
      }
  }
  
  public class Parent{ }
  
  public class Child : Parent { }
  </code></pre>
  
  <p>If an <code>int</code> can be implicitly cast to a <code>long</code>, the same way a <code>Child</code> can be implicitly cast to a <code>Parent</code>, why does the cast not work for an <code>IEnumerable&lt;int&gt;</code> to <code>IEnumerable&lt;long&gt;</code>?</p>
  
  <p>I always thought covariance made sense because you're converting one set of elements which are each individually able to be casted to another set of elements, and you're unable to add any new elements to this less derived collection meaning it's safe to cast it back again, because there was no chance of adding say a <code>SomeOtherChild</code> to the <code>Parent</code> collection.</p>
  
  <p>Maybe I'm missing the point of covariance, but if what I said is right, then why doesn't covariance allow the cast from <code>IEnumerable&lt;int&gt;</code> to <code>IEnumerable&lt;long&gt;</code>?</p>