协方差和上溯造型的区别 [英] Difference between covariance and upcasting

查看:135
本文介绍了协方差和上溯造型的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是协方差和上溯造型,或者,之间的差异更确切地说,他们为什么要给予不同的名字呢?

我见过被称为向上转型下面的例子:

 字符串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 type IEnumerable<Derived>. Let's say you have a variable baseSequence of type IEnumerable<Base>. Here, Derived derives from Base. 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 from IEnumerable<Base>. Rather, it is covariance that allows you to assign the value of the variable derivedSequence to the variable baseSequence. The idea is that variables of type Base can be assigned from objects of type Derived, and since IEnumerable<T> is covariant in its parameter, objects of type IEnumerable<Derived> can be assigned to variables of type IEnumerable<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 by F<T>; given a type T its image under the mapping F is F<T>.) Let's say that this mapping has the following very special property:

if X is assignment compatible with Y, then F<X> is assignment compatible with F<Y> as well.

In this case, we say that F is covariant in its parameter T. (Here, to say that "A is assignment compatible with B" where A and B are reference types means that instances of B can be stored in variables of type A.)

In our case, IEnumerable<T> in C# 4.0, an implicit reference conversion from instances of IEnumerable<Derived> to IEnumerable<Base> if Derived is derived from Base. The direction of assignment compatibility is preserved, and this is why we say that IEnumerable<T> is covariant in its type parameter.

这篇关于协方差和上溯造型的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆