liveBook · Manning

Covariant generic interface

As explained in Fabulous Adventures in Data Structures and Algorithms

A covariant generic type is a generic type whose type-parameter relationship preserves the direction of assignment compatibility.

A covariant generic interface preserves assignment compatibility: when Giraffe is assignable to Animal, IEnumerable<Giraffe> can be assigned to IEnumerable<Animal>. C# permits this only for certain interfaces and delegates, with a reference-type varying argument. section 2.3

Safety depends on direction of use. A covariant interface may produce values of T, but it must not accept T as an input. IEnumerable<T> is therefore eligible, while mutable IList<T> is not: treating IList<Tiger> as IList<Animal> would allow inserting a Giraffe into a tiger list. section 2.3

A covariant immutable stack

An immutable stack can declare interface IImStack<out T> : IEnumerable<T>, expose T Peek(), and return another IImStack<T> from Pop(). A stack of Tigers can then be viewed as a stack of Animals; pushing a Giraffe through the Animal view creates a new stack rather than mutating the original. section 2.3

In type theory, a generic interface acts like a function from types to types that preserves assignment-compatibility arrows. This makes it a covariant endofunctor when the source and target category are both the category of types. section 7.2 section 7.4

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Explains assignment behavior, mutation hazards, C# restrictions, and the type-theoretic endofunctor connection.
Examples
Uses Giraffe, Tiger, and Animal assignments, a hypothetical IList<T> insertion, and a covariant immutable stack.
What it assumes
Builds from derived-type assignment and generic interfaces before connecting them to assignment-compatibility morphisms.
What you can do after
Apply it via C# generic interfaces to widen read-only or immutable abstractions without violating type safety.
How it connects
Connects immutable stacks, IEnumerable<T>, IList<T>, delegates, assignment compatibility, and category theory.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.