Is C# 4.0 Tuple covariant(C# 4.0 元组是协变的吗)
问题描述
(我会自己检查一下,但我还没有 VS2010)
(I would check this out for myself, but I don't have VS2010 (yet))
假设我有 2 个基本接口:
Say I have 2 base interfaces:
IBaseModelInterface
IBaseViewInterface
以及实现这些的 2 个接口:
And 2 interfaces realizing those:
ISubModelInterface : IBaseModelInterface
ISubViewInterface : IBaseViewInterface
如果我定义一个 Tuple
我想根据返回 Tuple
的工厂的结果来设置它.
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 中,即使子接口实现了基本接口,我也无法做到这一点.而且我很确定如果我使用的是 IEnumerable<IBaseModelInterface>
,C# 4 允许我这样做,因为它现在使用 in
关键字定义以允许协方差.那么 Tuple
是否允许我这样做?
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?
推荐答案
Tuple
是一个类(嗯,一个类族)——它在定义上是不变的.正如您稍后提到的,只有接口和委托类型支持 .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 中取出"值.
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 元组是协变的吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# 4.0 元组是协变的吗
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01