#574 – The Problem with Array Covariance

If we have an array of a particular base type,  assignment compatibility allows us to assign any subtype to an element of the array.

            // Dog[] can contain
            //   Dogs, or sub-classes of Dogs
            Dog[] dogs = new Dog[3];
            dogs[0] = new Dog("Lassie");
            dogs[1] = new Terrier("Jack", "Crabby");
            dogs[2] = new BorderCollie("Kirby", "Ball");

Notice, however, that the array is still of type Dog[].

Array covariance allows us to assign an array of a subtype of a class to a variable whose type is an array of the base type.

Dog[] dogs = new Terrier[3];

This can lead to a problem. What looks syntactically like an array of Dogs is really an array of Terriers.  So if we try putting any other Dog into the array, the code will compile, but we’ll get an error at run-time.

dogs[0] = new Terrier("Jack", "Snarly");      // ok
dogs[1] = new BorderCollie("Kirby", "Ball");  // Run-time error !

Advertisements

#573 – Array Covariance Doesn’t Apply to Value Types

Array covariance allows T[] to be assigned to U[], if can be assigned to U.

// Assignment compatibility, because Terrier is sub-type of Dog
Terrier t = new Terrier("Bob");
Dog d = t;

// Allowed because of array covariance
Terrier[] terriers = MakeTerrierArray();
Dog[] dogs = terriers;

This does not work, however, if the contents of the arrays are value types.  Arrays of value-typed objects are not covariant.

            byte b1 = 12;
            ushort u1 = b1;  // Assignment compatible

            byte[] bytearray = new byte[] { 1, 2, 3 };

            // Not allowed.  Compile-time error "Cannot implicitly convert type 'byte[]' to 'ushort[]'
            ushort[] shortarray = bytearray;

#572 – Why Array Covariance Is Called Covariance

Array covariance in C# allows you to assign an array of instances of a derived class to a variable whose type is an array of instances of the base class.

Dog[] dogs = hounds;     // Where hounds is Hound[] and Hound is subclass of Dog

Covariance says that the ordering of two elements in a set is preserved after transforming each by the same function.

With array covariance, we can think of the “ordering” as being the fact that the subtype is narrower than the base class, which means that the assignment is allowed due to assignment compatibility.

The covariant function being applied to each type is to create an array of that type.  This “function”, an array of a type, is then covariant because if type T is narrower than type U, then T[] is also narrower than U[], preserving assignment compatibility.

#571 – Covariance in Programming Languages

In programming languages, the idea of covariance has to do with whether the ordering of a set of elements is preserved after calling some function that transforms each element.

Consider a set of elements and a function F that accepts as input a member of the set and returns a member of the set.  I.e. X’ = F(X), where both X and X’ are members of the set.

We describe a function as covariant if preserves the ordering of elements of the set passed to it.  If we have two members of our set, X and Y, and X <= Y, then the function F is covariant if F(X) <= F(Y).

For example, the function F(X) = 2X is covariant with respect to the set of integers.  If X <= Y, then 2X <= 2Y, for any X and Y integer values that you pick.

#568 – Array Covariance

In C#, you can always implicitly convert an instance of a more derived type  to an instance of a base type.

For example, the following is allowed:

Hound huck = new Hound("Huckleberry", 55);

// Since Hound is a sub-class of Dog, we can
// assign to Dog
Dog someDog = huck;

Note that at this point, the someDog variable points to an instance of a Hound, rather than an instance of a Dog.

You can also assign an array of objects of a more derived type to an array of objects of a base type.  This is known as array covariance.  It’s allowed as long as the type of the source array elements is implicitly convertible to the type of the target array elements.

Hound[] hounds = new Hound[2] {
new Hound("Huckleberry", 55),
new Hound("Astro", 50)};

// Allowed because of array covariance
Dog[] dogs = hounds;