#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.

#570 – Assignment Compatibility for Reference Types

A reference type is assignment compatible if the value being assigned belongs to a type that is either the same as, or is a derived type of, the type of the storage location being assigned to.  You can assign a variable of type T to a storage location of type U if T is a narrower type than U, or is the same type as U.

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

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

In this case, the variable of type Dog will still be pointing to an instance of a Hound.  The assignment doesn’t change anything about the object.

To see this, construct an instance of a Dog directly and then compare the objects.

// This one points to an actual Dog instance
Dog dog2 = new Dog("Just some dog", 2);

#569 – Assignment Compatibility

The idea of assignment compatibility in C# is the idea that you can store a value that has a particular type into a storage location (variable) of a different type without losing any data.  (The conversion is “representation-preserving“).

So we can say that type T is assignment compatible with type U if we can store values of type T into variables of type U.

For value types, a type T will typically be assignment compatible with another type U, if T can represent a subset of the values that U can represent.  T can be thought of as “smaller” or “narrower” than U.

// byte is assignment compatible with ushort
byte n1 = 123;    // byte: 0-255
ushort n2 = n1;   // ushort: 0-65535

As you’d expect, a type is always assignment compatible with itself:

            byte n1 = 123;
            byte n2 = n1;

#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;

#567 – Wider vs. Narrower Types

In a object-oriented programming language like C#, due to inheritance, we often end up with a hierarchy of types.

Types lower down in the diagram above are known as derived classes and the class above them, which they inherit from, is known as their base class.  For example, Working is a derived class with respect to Dog, which is its base class.  Notice that a class can be both a derived class and a base class for another class.  (E.g. Working serves as a base class for Boxer).

We also refer to types higher up in the class hierarchy as wider and types further down as narrower.  The Dog class is wider than the Terrier class in the sense that there are more dogs than there are Terriers.  You can also think of the narrower classes as being more specialized–Terrier is a specific type of Dog.

#566 – Implicit Conversions to Nullable Types

A nullable type represents a type whose value can be either a particular value type or can be the null value.

int i = 12;   // regular int, can't be null

int? j = 22;  // Nullable int, can store an int value
j = null;     // Can also store null value

You can implicitly convert from the corresponding value type to its matching nullable type.  For example, you can convert from an object of type int to an object of type int?

int i = 12;
int? j = i;   // Implicit conversion from int to int?

float f1 = i;   // Implicit conversion from int to float
float? f2 = i;  // Implicit conversion from int -> float -> float?

You can also implicitly convert from the null value to any nullable type.

int? i = null;