协方差和逆变现实世界的例子(Covariance and contravariance real world example)

编程入门 行业动态 更新时间:2024-10-19 12:32:55
协方差和逆变现实世界的例子(Covariance and contravariance real world example)

我有点麻烦,了解如何在现实世界中使用协方差和逆变。

到目前为止,我看到的唯一的例子是同样的旧数组示例。

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());

更多推荐

本文发布于:2023-08-04 12:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:协方差   例子   逆变   现实   世界

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!