协方差和上溯造型的区别 [英] Difference between covariance and upcasting
问题描述
什么是协方差和上溯造型,或者,之间的差异更确切地说,他们为什么要给予不同的名字呢?
我见过被称为向上转型下面的例子:
字符串s =你好;
对象o = S; //向上转型为'字符串'到'对象'
然而,下面我见过所谓的'协':
的String [] S =新的字符串[100];
[对象] O = S;IEnumerable的<串GT; IES =新的List<串GT;();
IEnumerable的<对象> IEO = IES;
现在,我的未经训练的眼,协方差似乎是一样向上转型,除了它是指集合的铸造。 (和类似的声明,可制成关于逆变和向下转换)。
是不是真的那么简单吗?
现在,我的未经训练的眼,协方差似乎是一样向上转型,除了它是指集合的铸造。 (和类似的声明,可制成关于逆变和向下转换)。
是不是真的那么简单吗?
块引用>协方差是不是上溯造型,虽然我可以看到为什么你认为这是相关的。
协方差是有关以下非常简单的想法。比方说,你有类型的变量
derivedSequence
的IEnumerable<衍生GT;
。比方说,你有类型的变量baseSequence
的IEnumerable<基地以及GT;
。在这里,导出
从基本
派生的。然后,协方差,下面是一个合法的分配和隐式引用转换发生:baseSequence = derivedSequence;
请注意,这不是上溯造型。这并不是说
的IEnumerable&LT的情况下,衍生>
从派生的IEnumerable<基地>
。相反,它是协方差,允许你指定变量derivedSequence
来变量baseSequence
的价值。我们的想法是该类型的变量基本
可以从类型的对象被分配派生
,自的IEnumerable< T>
是它的参数协变,类型的对象的IEnumerable<衍生>
可分配给类型的变量的IEnumerable<基本方式>
当然,我还没有真正解释了协方差为。一般情况下,协方差是有关以下简单的想法。比方说,你有一个映射
˚F
从类型类型(我会记为这个映射F< T>
;给出的类型T
的映射下的图像˚F
是F< T>
),假设这个映射具有以下非常特殊的属性。
如果
X
是分配与是
兼容,那么F< X>
是分配兼容F< Y方式>
以及
块引用>在这种情况下,我们说
˚F
是它的参数T
协变的。 (这里说A
是分配兼容B
,其中A
和B
是引用类型意味着B
的实例可以存储在类型的变量A
)在我们的例子中,
的IEnumerable< T>
在C#4.0中,从的IEnumerable&LT的实例隐式引用转换;衍生GT;
到的IEnumerable<基地>
如果派生
是基础衍生
。分配兼容性的方向是preserved,这就是为什么我们说的IEnumerable< T>
是在其类型参数协What is the difference between covariance and upcasting, or, more specifically, why are they given different names?
I've seen the following example referred to as 'upcasting':
string s = "hello"; object o = s; //upcast to 'string' to 'object'
Whereas, the following I have seen called 'covariance':
string[] s = new string[100]; object[] o = s; IEnumerable<string> ies = new List<string>(); IEnumerable<object> ieo = ies;
Now, to my untrained eye, covariance seems to be the same as upcasting, except that it refers the casting of collections. (And of a similar statement can be made regarding contravariance and downcasting).
Is it really that simple?
解决方案Now, to my untrained eye, covariance seems to be the same as upcasting, except that it refers the casting of collections. (And of a similar statement can be made regarding contravariance and downcasting).
Is it really that simple?
Covariance isn't about upcasting, although I can see why you think it's related.
Covariance is about the following very simple idea. Let's say you have a variable
derivedSequence
of typeIEnumerable<Derived>
. Let's say you have a variablebaseSequence
of typeIEnumerable<Base>
. Here,Derived
derives fromBase
. Then, with covariance, the following is a legal assignment, and an implicit reference conversion occurs:baseSequence = derivedSequence;
Note that this is not upcasting. It is not the case that
IEnumerable<Derived>
derives fromIEnumerable<Base>
. Rather, it is covariance that allows you to assign the value of the variablederivedSequence
to the variablebaseSequence
. The idea is that variables of typeBase
can be assigned from objects of typeDerived
, and sinceIEnumerable<T>
is covariant in its parameter, objects of typeIEnumerable<Derived>
can be assigned to variables of typeIEnumerable<Base>
.Of course, I haven't yet really explained what covariance is. In general, covariance is about the following simple idea. Let's say you have a mapping
F
from types to types (I'll denote this mapping byF<T>
; given a typeT
its image under the mappingF
isF<T>
.) Let's say that this mapping has the following very special property:if
X
is assignment compatible withY
, thenF<X>
is assignment compatible withF<Y>
as well.In this case, we say that
F
is covariant in its parameterT
. (Here, to say that "A
is assignment compatible withB
" whereA
andB
are reference types means that instances ofB
can be stored in variables of typeA
.)In our case,
IEnumerable<T>
in C# 4.0, an implicit reference conversion from instances ofIEnumerable<Derived>
toIEnumerable<Base>
ifDerived
is derived fromBase
. The direction of assignment compatibility is preserved, and this is why we say thatIEnumerable<T>
is covariant in its type parameter.这篇关于协方差和上溯造型的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!