C# 4.0 元组是协变的吗

发布于 2024-09-01 22:17:41 字数 733 浏览 3 评论 0原文

(我会自己检查一下,但我还没有 VS2010)

假设我有 2 个基本接口:

IBaseModelInterface
IBaseViewInterface

和 2 个实现这些接口的接口:

ISubModelInterface : IBaseModelInterface
ISubViewInterface : IBaseViewInterface

如果我定义一个 Tuple我想根据返回 Tuple 的工厂的结果来设置它。

在 C# 3 中,即使子接口实现了基本接口,我也无法执行此操作。我非常确定,如果我使用 IEnumerable,C# 4 允许我执行此操作,因为它现在使用 in 关键字定义以允许协变。那么 Tuple 允许我这样做吗?

据我了解,协变仅在接口上允许,所以这是否意味着需要有一个 ITuple 接口?这存在吗?

(I would check this out for myself, but I don't have VS2010 (yet))

Say I have 2 base interfaces:

IBaseModelInterface
IBaseViewInterface

And 2 interfaces realizing those:

ISubModelInterface : IBaseModelInterface
ISubViewInterface : IBaseViewInterface

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>.

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始于初秋 2024-09-08 22:17:41

Tuple 是一个类(嗯,一个类家族)——根据定义它是不变的。正如您稍后提到的,只有接口和委托类型支持 .NET 4 中的通用差异。

据我所知,没有 ITuple 接口。可能存在协变的元组,因为元组是不可变的,因此您只能从 API“外部”获取值。

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.

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.

余生共白头 2024-09-08 22:17:41

您可以继承元组来创建自己的协变元组。这样您就不必重写自己的相等逻辑。

public interface ICovariantTuple<out T1>
{
    T1 Item1 { get; }
}
public class CovariantTuple<T1> : Tuple<T1>, ICovariantTuple<T1>
{
    public CovariantTuple(T1 item1) : base(item1) { }
}

public interface ICovariantTuple<out T1, out T2>
{
    T1 Item1 { get; }
    T2 Item2 { get; }
}
public class CovariantTuple<T1, T2> : Tuple<T1, T2>, ICovariantTuple<T1, T2>
{
    public CovariantTuple(T1 item1, T2 item2) : base(item1, item2) { }
}

etc.... for 3, 4, 5, 6, 7, 8 items

编译失败

Tuple<Exception> item = new Tuple<ArgumentNullException>(null);

编译成功

ICovariantTuple<Exception> item = new CovariantTuple<ArgumentNullException>(null);

8 项后没有基元组,但应该足够了。

You can inherit from tuple for create your own Covariant Tuple. This way you avoid to have to rewrite your own equalities logic.

public interface ICovariantTuple<out T1>
{
    T1 Item1 { get; }
}
public class CovariantTuple<T1> : Tuple<T1>, ICovariantTuple<T1>
{
    public CovariantTuple(T1 item1) : base(item1) { }
}

public interface ICovariantTuple<out T1, out T2>
{
    T1 Item1 { get; }
    T2 Item2 { get; }
}
public class CovariantTuple<T1, T2> : Tuple<T1, T2>, ICovariantTuple<T1, T2>
{
    public CovariantTuple(T1 item1, T2 item2) : base(item1, item2) { }
}

etc.... for 3, 4, 5, 6, 7, 8 items

Compile Fail

Tuple<Exception> item = new Tuple<ArgumentNullException>(null);

Compile Success

ICovariantTuple<Exception> item = new CovariantTuple<ArgumentNullException>(null);

There is no base Tuple after 8 items, but it should be enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文