Covariance and contravariance real world example

后端 未结 9 1992
醉梦人生
醉梦人生 2020-11-22 16:57

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 o

9条回答
  •  轮回少年
    2020-11-22 17:43

    Let's say you have a class Person and a class that derives from it, Teacher. You have some operations that take an IEnumerable as the argument. In your School class you have a method that returns an IEnumerable. Covariance allows you to directly use that result for the methods that take an IEnumerable, 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 people) { ... }
    }
    
    public class School
    {
        public IEnumerable GetTeachers() { ... }
    }
    
    public class PersonNameComparer : IComparer
    {
        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 constructor uses a contravariant interface, IComparer,
    // 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, new PersonNameComparer());
    

提交回复
热议问题