仍然感到困惑的协变和逆变&安培;进出 [英] still confused about covariance and contravariance & in/out
问题描述
好吧,我读上计算器这个话题了一下,看着这&放大器; <一href=\"http://channel9.msdn.com/posts/bruceky/Whirlwind-13-Whats-new-in-C-4-Covariance--Contravariance/\">this,但还是有点困惑CO /禁忌差异。
ok i read a bit on this topic on stackoverflow, watched this & this, but still a bit confused about co/contra-variance.
从<一个href=\"http://stackoverflow.com/questions/1078423/c-is-variance-covariance-contravariance-another-word-for-polymorphism/1078469#1078469\">here
协方差允许做大(少
特异性的)输入一个要被取代
API在原始类型只
在一个输出位置使用(例如作为
一个返回值)。逆变允许
一个小(更具体的)类型为
一个API中取代,其中
原始类型只在使用
输入的位置。
Covariance allows a "bigger" (less specific) type to be substituted in an API where the original type is only used in an "output" position (e.g. as a return value). Contravariance allows a "smaller" (more specific) type to be substituted in an API where the original type is only used in an "input" position.
我知道它与类型安全的事。
i know it has to do with type safety.
有关 /缩小
的事情。我可以说我在当我需要写微博,和退出
时,其只读使用。和
在
表示禁忌方差,退出
协方差。但是从上面的说明...
about the in/out
thing. can i say i use in
when i need to write to it, and out
when its read only. and in
means contra-variance, out
co-variance. but from the explanation above...
和<一个href=\"http://stackoverflow.com/questions/1078423/c-is-variance-covariance-contravariance-another-word-for-polymorphism\">here
例如,一个列表与LT;香蕉&GT;
不能
水果GT; ,因为作为一个名单,LT处理
list.Add(新的苹果())
是有效的
列表而不是列表&LT;香蕉方式&gt;
For example, a
List<Banana>
can't be treated as aList<Fruit>
becauselist.Add(new Apple())
is valid for List but not forList<Banana>
.
所以应该不会是,如果我在使用
/我要去写的对象,它必须是更大更通用。
so shouldn't it be, if i were to use in
/ am going to write to the object, it must be bigger more generic.
我知道这个问题已经被问,但仍然很困惑。
i know this question has been asked but still very confused.
推荐答案
这两个协变和逆变在C#4.0指的是使用一个派生类,而不是基类的能力。输入/输出关键字是编译器提示来指示类型参数是否会被用于输入和输出。
Both covariance and contravariance in C# 4.0 refer to the ability of using a derived class instead of base class. The in/out keywords are compiler hints to indicate whether or not the type parameters will be used for input and output.
协方差在C#4.0是由援建了
关键字,这意味着使用一个派生类的退出$ C $的泛型类型C>类型参数的确定。因此,
Covariance in C# 4.0 is aided by out
keyword and it means that a generic type using a derived class of the out
type parameter is OK. Hence
IEnumerable<Fruit> fruit = new List<Apple>();
由于苹果
是水果
,列表&LT;苹果&GT;
可以安全地用作的IEnumerable&LT;水果GT;
逆变是在
关键字,它表示输入类型,通常代表。其原理是相同的,这意味着委托可以接受更多的派生类。
Contravariance is the in
keyword and it denotes input types, usually in delegates. The principle is the same, it means that the delegate can accept more derived class.
public delegate void Func<in T>(T param);
这意味着,如果我们有一个 Func键&LT;水果GT;
,它可转化为 Func键&LT;苹果&GT;
This means that if we have a Func<Fruit>
, it can be converted to Func<Apple>
.
Func<Fruit> fruitFunc = (fruit)=>{};
Func<Apple> appleFunc = fruitFunc;
为什么他们叫CO /逆变如果他们基本上是同样的事情?
因为尽管原理是一样的,安全的铸造来源于基地,对输入类型使用时,我们可以放心地投了派生程度较小的类型( Func键&LT;水果GT;
),以更派生类型( Func键&LT;苹果&GT;
),这是有道理的,因为这需要的任何功能水果
,也可以采取苹果
。
Why are they called co/contravariance if they are basically the same thing?
Because even though the principle is the same, safe casting from derived to base, when used on the input types, we can safely cast a less derived type (Func<Fruit>
) to a more derived type (Func<Apple>
), which makes sense, since any function that takes Fruit
, can also take Apple
.
这篇关于仍然感到困惑的协变和逆变&安培;进出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!