我有点麻烦,了解如何在现实世界中使用协方差和逆变。
到目前为止,我看到的唯一的例子是同样的旧数组示例。
object[] objectArray = new string[] { "string 1", "string 2" };看到一个例子可以让我在开发过程中使用它,如果我能看到它在其他地方被使用,那将是很好的。
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.
最满意答案
假设你有一个班级的人和一个派生的班,老师。 您有一些操作可以使用IEnumerable<Person>作为参数。 在您的学校课程中,您有一种返回IEnumerable<Teacher> 。 协方差允许您直接将该结果用于采用IEnumerable<Person>的方法,将更多派生类型替换为更少派生(更通用)的类型。 反差,反直觉,允许您使用更通用的类型,其中指定了更多的派生类型。 另请参见https://msdn.microsoft.com/en-us/library/dd799517.aspx
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); } } ... 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<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());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<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());更多推荐
发布评论