是C#4.0元组协变 [英] Is C# 4.0 Tuple covariant
问题描述
(我会检查了这一点为自己,但我没有VS2010(还))
(I would check this out for myself, but I don't have VS2010 (yet))
说我有2个基本接口:
IBaseModelInterface
IBaseViewInterface
和2个接口实现的:
ISubModelInterface : IBaseModelInterface
ISubViewInterface : IBaseViewInterface
如果我定义了一个元组LT; IBaseModelInterface,IBaseViewInterface>
我想设置基于一个工厂,返回的结果元组LT; ISubModelInterface,ISubViewInterface>
。
If I define a Tuple<IBaseModelInterface, IBaseViewInterface>
I would like to set that based on the result of a factory that returns Tuple<ISubModelInterface, ISubViewInterface>
.
在C#3,我不能做到这一点,即使子接口实现的基本接口。而且我敢肯定,C#4让我,如果我是用做的IEnumerable< IBaseModelInterface>
,因为它与在现在定义
关键字允许协方差。所以确实元组
让我做到这一点?
In C# 3 I can't do this even though the sub interfaces realize the base interfaces. And I'm pretty sure C# 4 lets me do this if I was using IEnumerable<IBaseModelInterface>
because it's now defined with the in
keyword to allow covariance. So does Tuple
allow me to do this?
这是什么(小)据我了解,协方差只允许在接口,这是否意味着需要有一个 ITuple< T1,T2>
界面?这是否存在?
From what (little) I understand, covariance is only allowed on interfaces, so does that mean there needs to be an ITuple<T1, T2>
interface? Does this exist?
推荐答案
元组
是一个类(当然,一类家庭) - 它的定义不变。正如你提到以后,只有接口和委托类型支持.NET 4的通用差异。
Tuple
is a class (well, a family of classes) - it's invariant by definition. As you mention later on, only interfaces and delegate types support generic variance in .NET 4.
有没有 ITuple
界面,我所知道的。有可能是其中之一将是协变,因为元组是不可变的,所以你只能获得API值OUT。
There's no ITuple
interface that I'm aware of. There could be one which would be covariant, as the tuples are immutable so you only get values "out" of the API.
这篇关于是C#4.0元组协变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!