Broken Covariance for Array Types

Since the .NET Framework was released, arrays have been covariant. Going back to our definition of variance annotations, there was a mention of “constructed types.” Array types are such animals because any element type can be used to construct an array type. For example, given the type int, we can construct an array that can hold integer values (that is, which is of type int[]). And this by itself is a new type we can create an array type out of, namely a jagged int[][] array.

Our next duty is to express a subtyping relationship between array types based on the knowledge of a subtyping relationship between the element types. For example, if we know that every Apple is a Fruit, can we treat every Apple[] as a ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.