如何为通用协方差和放大器;孔特拉方差实现在C#4.0吗? [英] How is Generic Covariance & Contra-variance Implemented in C# 4.0?

查看:151
本文介绍了如何为通用协方差和放大器;孔特拉方差实现在C#4.0吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有参加2008年PDC,但我听到了一些消息,C#4.0中宣布支持通用协方差和反方差。也就是说,列表<串> 可分配给列表<对象> 。怎么可能?

I didn't attend PDC 2008, but I heard some news that C# 4.0 is announced to support Generic covariance and contra-variance. That is, List<string> can be assigned to List<object>. How could that be?

在乔恩斯基特的书 详解C#,它解释了为什么C#泛型不支持协方差和反方差。它主要是用于编写安全code。现在,C#4.0改为支持他们。难道带来混乱?

In Jon Skeet's book C# in Depth, it is explained why C# generics doesn't support covariance and contra-variance. It is mainly for writing secure code. Now, C# 4.0 changed to support them. Would it bring chaos?

任何人都知道C#4.0的细节可以提供一些解释?

Anybody know the details about C# 4.0 can give some explanation?

推荐答案

差异只会在安全的方式支持 - 事实上,使用CLR的已经有能力。于是例子,我给在本书试图用列表&LT;香蕉&GT; 列表&LT;水果GT; (或不管它是什么),仍然是行不通的 - 但是还有一些其他的方案将

Variance will only be supported in a safe way - in fact, using the abilities that the CLR already has. So the examples I give in the book of trying to use a List<Banana> as a List<Fruit> (or whatever it was) still won't work - but a few other scenarios will.

首先,将只支持接口和委托。

Firstly, it will only be supported for interfaces and delegates.

其次,它要求接口/委托笔者装饰类型参数为(用于逆变)或退出(协方差)。最明显的例子就是的IEnumerable&LT; T&GT; 这只会让你把价值观走出去的它 - 它不会让你添加新的。这将成为的IEnumerable&LT;出T&GT; 。这不会伤害类型安全可言,但可让您返回的IEnumerable&LT;串&GT; 从宣布回归的方法的IEnumerable&LT;对象&gt; 的实例。

Secondly, it requires the author of the interface/delegate to decorate the type parameters as in (for contravariance) or out (for covariance). The most obvious example is IEnumerable<T> which only ever lets you take values "out" of it - it doesn't let you add new ones. That will become IEnumerable<out T>. That doesn't hurt type safety at all, but lets you return an IEnumerable<string> from a method declared to return IEnumerable<object> for instance.

逆变是很难给出具体的例子使用的接口,但它很容易与委托。考虑动作&LT; T&GT; - 这只是重新presents这需要一个 T 参数的方法。这将是很好的能够无缝地转换使用动作&LT;对象&gt; 动作&LT;串&GT; - 任何方法,它需要一个对象参数是要被罚款时,它的使用字符串而不是psented $ p $。当然,C#2已具有协方差和代表在一定程度上的逆变,但通过从一个委托类型到另一种实际的转化(创建新实例) - 见P141-144的例子。 C#4将使这一更加通用的,和(I相信)将避免对于转换创建一个新的实例。 (这将是一个参考,而不是转换)。

Contravariance is harder to give concrete examples for using interfaces, but it's easy with a delegate. Consider Action<T> - that just represents a method which takes a T parameter. It would be nice to be able to convert seamlessly use an Action<object> as an Action<string> - any method which takes an object parameter is going to be fine when it's presented with a string instead. Of course, C# 2 already has covariance and contravariance of delegates to some extent, but via an actual conversion from one delegate type to another (creating a new instance) - see P141-144 for examples. C# 4 will make this more generic, and (I believe) will avoid creating a new instance for the conversion. (It'll be a reference conversion instead.)

希望这会清除它一点 - !请让我知道,如果它没有意义。

Hope this clears it up a bit - please let me know if it doesn't make sense!

这篇关于如何为通用协方差和放大器;孔特拉方差实现在C#4.0吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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