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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.