Resources on Covariance and Contravariance

Eric Lippert is on the C# Compiler team and has written a series of posts on this topic that are an absolute must-read!

  1. Part One
  2. Part Two: Array Covariance (and Why is covariance of value-typed arrays inconsistent?)
  3. Part Three: Method Group Conversion Variance
  4. Part Four: Real Delegate Variance
  5. Part Five: Higher Order Functions Hurt My Brain
  6. Part Six: Interface Variance
  7. Part Seven: Why Do We Need A Syntax
  8. Part Eight: Syntax Options
  9. Part Nine: Breaking Changes
  10. Part Ten: Dealing With Ambiguity
  11. Part Eleven: To infinity, but not beyond


The information here is quite “flat”, but still worth a read.


Category Theory

The names covariance and contravariance in C# derive from co- and contravariant functors described in category theory.

Hope that helps!


.NET 4.0 types with added support for co- or contravariance

Just a plain list of types from the .NET 4.0 Base Class Library where Microsoft has added support for covariance (out) and contravariance (in).

in System

// Delegates
Action<in T1[ T16]>
Func<out TResult>
Func<in T1[ T16], out TResult>
Comparison<in T>
Converter<in TInput, out TOutput>
Predicate<in T> 

// Interfaces
IComparable<in T>
IObservable<out T>
IObserver<in T>

in System.Collections.Generic

IComparer<in T> 
IEnumerable<out T> 
IEnumerator<out T> 
IEqualityComparer<in T> 

in System.Linq

IGrouping<out TKey,out TElement> 
IOrderedQueryable<out T> 
IQueryable<out T>