Why does the backing store of an array bind to the smallest type?
<p>In C# it is legal to write</p>

<pre><code>Animal[] a = new Giraffe[4]; //with obvious relationships
</code></pre>

<p>Because arrays are covariant.  However, this is a breaking relationship.  It's a runtime exception to then apply a different type of Animal to this array because the backing store is a <code>Giraffe</code>.  </p>

<pre><code>a[0] = new Cat(); //KABOOM
</code></pre>

<p>Why doesn't the language create a covariant backing store?  This doesn't appear to be a limitation of the language because I can create this type of data structure myself:</p>

<pre><code>class Program
{
    static void Main(string[] args)
    {
        A[] a = new B[2];
        a[0] = new B();
        a[1] = new C();//runtime exception

        var containerLegal = new Container&lt;B, A&gt;();
        containerLegal.Add(new B());
        containerLegal.Add(new C());//works fine
    }
}

class A { }

class B : A { }

class C : A { }

class Container&lt;T,S&gt; where T: class, S
{
    S[] s = new S[4];
    int currentIndex = 0;

    public void Add(S t)
    {
        s[currentIndex] = t;
        currentIndex++;
    }
}
</code></pre>

<p>I'm cheating a little bit cause I'm not using <code>T</code> in <code>Container&lt;&gt;</code>...but I think its a fair relationship anyhow.  </p>
