## #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;
dogs = new Dog("Lassie");
dogs = new Terrier("Jack", "Crabby");
dogs = 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;
```

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 = new Terrier("Jack", "Snarly");      // ok
dogs = new BorderCollie("Kirby", "Ball");  // Run-time error !
``` ## #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.

## #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 {
new Hound("Huckleberry", 55),
new Hound("Astro", 50)};

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