.NET:值类型继承 - 技术限制?

发布于 2024-10-17 13:41:38 字数 162 浏览 2 评论 0原文

我想知道是否存在任何技术原因导致 .NET 值类型不支持继承(忽略接口实现)...我乍一看无法想到值类型不应允许单个基类继承的原因。

(我的意思是,可以说,如果最终得到一个巨大的继承层次结构,值类型的继承会很糟糕,但我主要想知道是否存在任何运行时限制而不是实际限制。)

谢谢。

I'm wondering if there are any technical reasons for why .NET value types do not support inheritance (disregarding interface implementation)... I can't at first glance think of a reason why value types shouldn't allow single base class inheritance.

(I mean, arguably, inheritance for value types would be bad if you end up with a huge inheritance hierarchy, but I'm mostly wondering if there are any runtime limitations rather than practical limitations.)

Thanks.

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

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

发布评论

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

评论(3

寄离 2024-10-24 13:41:38

考虑为值类型分配的内存。 CLR 确切地知道要为值类型的变量分配多少空间,因为它知道会有哪些字段。它不可能以具有更多字段的子类型值结束。

现在我们可以拥有值类型继承,这只是截断了一些东西:

ExtendedValueType evt = new ExtendedValueType(...);
BaseValueType bvt = evt;
// Now you couldn't cast back to ExtendedValueType, because we'd have lost
// information

同样,类型信息没有地方可以存在于值本身中,因此任何被扩展类型覆盖的虚拟方法都不会通过 调用bvt,因为就一切而言,该值只是 BaseValueType 的值。换句话说,许多“自然”继承特征将会缺失,我认为这会引起很多混乱。

Consider the memory allocated for a value type. The CLR knows exactly how much space to allocate for a variable of a value type, because it knows what fields there will be. It can't possibly end up with a subtype value with more fields.

Now we could have value type inheritance which just truncated things:

ExtendedValueType evt = new ExtendedValueType(...);
BaseValueType bvt = evt;
// Now you couldn't cast back to ExtendedValueType, because we'd have lost
// information

Likewise there's nowhere for type information to live in the value itself, so any virtual methods overridden by the extended type wouldn't be called via bvt, because as far as everything is concerned, the value is then just a value of BaseValueType. In other words, a lot of "natural" inheritance features would be missing in a way which I think would cause a lot of confusion.

作死小能手 2024-10-24 13:41:38

我认为值类型不支持继承的原因是它们在内存中的表示方式。值类型所表示的数据的大小取决于其组成字段。也就是说,如果您的值类型包含 int 和 string,则 32 位系统上的总大小将为 8,即 4(int 的大小)+ 4(指针的大小)。这意味着值类型在内存中表示为字节块,没有任何更多信息。

现在与类类型进行对比,它们都是指针大小,或者在 32 位系统上为 4。由于类类型的实例是指针,因此它们可以引用继承所需的内容,例如 VMT(虚拟方法表)和对父类信息的引用。这是值类型无法做到的,因此值类型不支持继承。

I believe the reason why value types do not support inheritance is due to how they are represented in memory. The size of and consequently the data represented by a value type is dependent on its constituent fields. That is, if your value type contains an int and a string, the total size on a 32 bit system would be 8, or 4 (size of int) + 4 (size of pointer). What this means is that value types are represented in memory are a block of bytes, without any more information.

Now contrast that with class types, they are all size of pointers, or 4 on 32 bit systems. Since instances of class types are pointers, they can then refer to the things you need for inheritance, like a VMT (virtual method table) and a reference to the parent classes information. This is something a value type can't do, and hence why value types don't support inheritance.

↙厌世 2024-10-24 13:41:38

假设可以做到。

您将能够重复使用某些实现。
但继承的真正好处是替换和多态。它们需要引用使用。

这就是支持接口实现的原因,因为它总是涉及装箱。但这不适用于继承。

Suppose it could be done.

You would be able to re-use some implementation.
But the real benefits of inheritance are Substitution and Polymorphism. They require use-by-reference.

And that is why interface implementation is supported because it always involves Boxing. But that would not do for inheritance.

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