为什么 .NET 4 中的类不是协变的?

发布于 2024-09-03 01:16:30 字数 918 浏览 7 评论 0原文

可能的重复:
为什么类中没有通用方差C# 4.0?

作为一名菜鸟程序员,我对 .NET 4 中的差异有几个问题。与其说是关于它如何工作,不如说是关于为什么某些东西没有差异以及其他人是否会发现这有用。

问题 1:

我知道接口和委托在 .NET 4 中可以是协变/逆变的,但为什么类不能呢?那么,问题 1:

List(of BaseClass) = List(of DerivedClass)

这是否不安全?这没有用吗?


问题 2:

问题 2 源自问题 1,但可能更多地涉及签名而不是方差。假设我有一个带有 MustOverride 成员的 MustInherit 类:

Public MustInherit Class TestBase
    Public MustOverride Property SomeClass as BaseClass
End Class

在派生类中,为什么我不能重写 SomeClass 并返回 BaseClass 的派生类?这不安全吗?难道只是签名不检查继承关系吗?

Public Class TestSpecific
    Inherits TestBase
    Public Overrides Property SomeClass as DerivedClass
End Class

如果您了解 .NET 4 中不允许这样做的任何见解,我们将不胜感激。

Possible Duplicate:
Why isn’t there generic variance for classes in C# 4.0?

As a rookie programmer I have a couple of questions about variance in .NET 4. Not so much about how it works, but why certain things are not variant and if other people would find this useful.

Question 1:

I know that interfaces and delegates can be covariant/contravariant in .NET 4, but why not classes? So, question 1:

List(of BaseClass) = List(of DerivedClass)

Is this somehow unsafe? Wouldn't this be useful?


Question 2:

Question 2 follows from question 1, but may deal more with signatures than variance. Suppose I have a MustInherit class with a MustOverride member:

Public MustInherit Class TestBase
    Public MustOverride Property SomeClass as BaseClass
End Class

In the derived class, why can't I override SomeClass and return a Derived Class Of BaseClass? Is this unsafe? Is it just that the signatures don't check for inheritance relationships?

Public Class TestSpecific
    Inherits TestBase
    Public Overrides Property SomeClass as DerivedClass
End Class

Any insight as to why this isn't allowed in .NET 4 would be appreciated.

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

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

发布评论

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

评论(1

抽个烟儿 2024-09-10 01:16:30

Eric Lippert 回答第一部分你的问题在这里

至于您问题的第二部分,C# 和 VB.NET 都要求返回类型协方差 - 但相对于其他语言功能,它的优先级还不够高,无法将其纳入任何版本。如果我没记错的话,此功能还需要对 CLR 进行更改才能正确实现。

至于为什么会这样,我将向 Eric Lippert 求助一下,并回答说,不实现某个功能是免费的,而实现某个功能则需要预算和时间来设计、开发、测试和记录它……这就是不免费。一项功能必须对足够多的人具有足够的价值,才能证明创建它的费用是合理的。

Eric Lippert answers the first part of you question here.

As for the second part of you question, return type covariance has been requested for both C# and VB.NET - however it has not yet been prioritized high-enough relative to other language features to make it into any release. If I remember correctly, this feature would also require changes to the CLR to be implemented appropriately.

As to why this is so, I'll channel Eric Lippert for a moment and respond that not implementing a feature is free, while implementing a features requires the budget and time to design, develop, test, and document it ... which is not free. A feature has to be sufficiently valuable to enough people to justify the expense of creating it.

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