#1,146 – Generics Don’t Support Covariance

In C#, arrays are covariant, so you can do the following:

            // Array covariance--OK
            Dog[] dogs = new Terrier[5];

Generics in C#, however, are not covariant (they are invariant). The following code will not compile.

            // Generic covariance--not OK (compiler error)
            List<Dog> moreDogs = new List<Terrier>();
Advertisements