Covariance & 之间的区别逆变 [英] Difference between Covariance & Contra-variance
问题描述
我无法理解协方差和逆变之间的区别.
I am having trouble understanding the difference between covariance and contravariance.
推荐答案
问题是协变和逆变有什么区别?"
The question is "what is the difference between covariance and contravariance?"
协方差和逆变是映射函数的属性,该函数将集合的一个成员与另一个成员相关联.更具体地说,相对于该集合上的关系,映射可以是协变的或逆变的.
Covariance and contravariance are properties of a mapping function that associates one member of a set with another. More specifically, a mapping can be covariant or contravariant with respect to a relation on that set.
考虑所有 C# 类型集合的以下两个子集.第一:
Consider the following two subsets of the set of all C# types. First:
{ Animal,
Tiger,
Fruit,
Banana }.
其次,这个明显相关的集合:
And second, this clearly related set:
{ IEnumerable<Animal>,
IEnumerable<Tiger>,
IEnumerable<Fruit>,
IEnumerable<Banana> }
从第一组到第二组有一个映射操作.也就是说,对于第一组中的每个T,第二组中的对应类型是IEnumerable
.或者,简而言之,映射是T → IE
.请注意,这是一个细箭头".
There is a mapping operation from the first set to the second set. That is, for each T in the first set, the corresponding type in the second set is IEnumerable<T>
. Or, in short form, the mapping is T → IE<T>
. Notice that this is a "thin arrow".
到目前为止和我在一起吗?
With me so far?
现在让我们考虑一个关系.第一组中的类型对之间存在赋值兼容关系.Tiger
类型的值可以分配给 Animal
类型的变量,因此这些类型被称为赋值兼容".让我们以较短的形式编写X
类型的值可以分配给 Y
类型的变量":X ⇒ Y
.请注意,这是一个粗箭头".
Now let's consider a relation. There is an assignment compatibility relationship between pairs of types in the first set. A value of type Tiger
can be assigned to a variable of type Animal
, so these types are said to be "assignment compatible". Let's write "a value of type X
can be assigned to a variable of type Y
" in a shorter form: X ⇒ Y
. Notice that this is a "fat arrow".
所以在我们的第一个子集中,这里是所有的分配兼容性关系:
So in our first subset, here are all the assignment compatibility relationships:
Tiger ⇒ Tiger
Tiger ⇒ Animal
Animal ⇒ Animal
Banana ⇒ Banana
Banana ⇒ Fruit
Fruit ⇒ Fruit
在支持某些接口协变赋值兼容性的C#4中,第二组类型对之间存在赋值兼容性关系:
In C# 4, which supports covariant assignment compatibility of certain interfaces, there is an assignment compatibility relationship between pairs of types in the second set:
IE<Tiger> ⇒ IE<Tiger>
IE<Tiger> ⇒ IE<Animal>
IE<Animal> ⇒ IE<Animal>
IE<Banana> ⇒ IE<Banana>
IE<Banana> ⇒ IE<Fruit>
IE<Fruit> ⇒ IE<Fruit>
注意映射T → IE
保留了赋值兼容性的存在和方向.也就是说,如果 X ⇒ Y
,那么 IE
.
Notice that the mapping T → IE<T>
preserves the existence and direction of assignment compatibility. That is, if X ⇒ Y
, then it is also true that IE<X> ⇒ IE<Y>
.
如果我们在粗箭头的两边有两个东西,那么我们可以用相应的细箭头右侧的东西替换两边.
If we have two things on either side of a fat arrow, then we can replace both sides with something on the right hand side of a corresponding thin arrow.
对于特定关系具有此属性的映射称为协变映射".这应该是有道理的:可以在需要一系列 Animals 的地方使用 Tigers 序列,但反之则不然.在需要老虎序列的地方不一定使用动物序列.
A mapping which has this property with respect to a particular relation is called a "covariant mapping". This should make sense: a sequence of Tigers can be used where a sequence of Animals is needed, but the opposite is not true. A sequence of animals cannot necessarily be used where a sequence of Tigers is needed.
这就是协方差.现在考虑所有类型集合的这个子集:
That's covariance. Now consider this subset of the set of all types:
{ IComparable<Tiger>,
IComparable<Animal>,
IComparable<Fruit>,
IComparable<Banana> }
现在我们有了从第一组到第三组的映射T → IC
.
now we have the mapping from the first set to the third set T → IC<T>
.
在 C# 4 中:
IC<Tiger> ⇒ IC<Tiger>
IC<Animal> ⇒ IC<Tiger> Backwards!
IC<Animal> ⇒ IC<Animal>
IC<Banana> ⇒ IC<Banana>
IC<Fruit> ⇒ IC<Banana> Backwards!
IC<Fruit> ⇒ IC<Fruit>
也就是说,映射T → IC
保留了存在但反转了分配兼容性的方向.也就是说,如果 X ⇒ Y
,则 IC
.
That is, the mapping T → IC<T>
has preserved the existence but reversed the direction of assignment compatibility. That is, if X ⇒ Y
, then IC<X> ⇐ IC<Y>
.
保留但反转关系的映射称为逆变映射.
同样,这应该是正确的.可以比较两只动物的设备也可以比较两只老虎,但是可以比较两只老虎的设备不一定可以比较任意两只动物.
Again, this should be clearly correct. A device which can compare two Animals can also compare two Tigers, but a device which can compare two Tigers cannot necessarily compare any two Animals.
这就是 C# 4 中协变和逆变之间的区别.协变保留可赋值的方向.逆变反转它.
So that's the difference between covariance and contravariance in C# 4. Covariance preserves the direction of assignability. Contravariance reverses it.
这篇关于Covariance & 之间的区别逆变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!