Search code examples
c#c#-4.0covariance

Covariance and contravariance real world example


I'm having a little trouble understanding how I would use covariance and contravariance in the real world.

So far, the only examples I've seen have been the same old array example.

object[] objectArray = new string[] { "string 1", "string 2" };

It would be nice to see an example that would allow me to use it during my development if I could see it being used elsewhere.


Solution

  • Let's say you have a class Person and a class that derives from it, Teacher. You have some operations that take an IEnumerable<Person> as the argument. In your School class you have a method that returns an IEnumerable<Teacher>. Covariance allows you to directly use that result for the methods that take an IEnumerable<Person>, substituting a more derived type for a less derived (more generic) type. Contravariance, counter-intuitively, allows you to use a more generic type, where a more derived type is specified.

    See also Covariance and Contravariance in Generics on MSDN.

    Classes:

    public class Person 
    {
         public string Name { get; set; }
    } 
    
    public class Teacher : Person { } 
    
    public class MailingList
    {
        public void Add(IEnumerable<out Person> people) { ... }
    }
    
    public class School
    {
        public IEnumerable<Teacher> GetTeachers() { ... }
    }
    
    public class PersonNameComparer : IComparer<Person>
    {
        public int Compare(Person a, Person b) 
        { 
            if (a == null) return b == null ? 0 : -1;
            return b == null ? 1 : Compare(a,b);
        }
    
        private int Compare(string a, string b)
        {
            if (a == null) return b == null ? 0 : -1;
            return b == null ? 1 : a.CompareTo(b);
        }
    }
    

    Usage:

    var teachers = school.GetTeachers();
    var mailingList = new MailingList();
    
    // Add() is covariant, we can use a more derived type
    mailingList.Add(teachers);
    
    // the Set<T> constructor uses a contravariant interface, IComparer<in T>,
    // we can use a more generic type than required.
    // See https://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx for declaration syntax
    var teacherSet = new SortedSet<Teachers>(teachers, new PersonNameComparer());