为什么 C# 结构不能被继承?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2310103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:18:09  来源:igfitidea点击:

Why C# structs cannot be inherited?

c#.net

提问by smwikipedia

I am reading CLR via C# by Jeffery Richter and it says a struct is a value type and cannot be inherited.

我正在通过 Jeffery Richter 的 C# 阅读 CLR,它说结构是一种值类型,不能被继承。

Are there any technical or philosophical reasons?

是否有任何技术或哲学原因?

采纳答案by Jesse Millikan

Edit: There are serious editorial concerns about this post, apparently. See comment section.

编辑:显然,这篇文章存在严重的编辑问题。见评论部分。

A little of both.

两者都有一点。

Philosophically, it works out - there are classes, which are the "real" building block for object oriented programming, and there are structs, which are lightweight data types for storage but allow object-like method calls for familiarity and convenience.

从哲学上讲,它是有效的——有类,它们是面向对象编程的“真正”构建块,还有结构,它们是用于存储的轻量级数据类型,但允许类对象方法调用以提高熟悉度和便利性。

Technically, being a "value type" means that the entire struct - all of its contents - are (usually) stored wherever you have a variable or member of that type. As a local variable or function parameter, that means on the stack. For member variables, that means stored entirely as part of the object.

从技术上讲,作为“值类型”意味着整个结构 - 它的所有内容 -(通常)都存储在您拥有该类型的变量或成员的任何地方。作为局部变量或函数参数,这意味着在堆栈上。对于成员变量,这意味着完全存储为对象的一部分。

As a (primary) example of why inheritance is a problem, consider how storage is affected at a low level if you allowed structs to have subtypes with more members. Anything storing that struct type would take up a variable amount of memory based on which subtype it ended up containing, which would be an allocation nightmare. An object of a given class would no longer have a constant, known size at compile time and the same would be true for stack frames of any method call. This does not happen for objects, which have storage allocated on the heap and instead have constant-sized references to that storage on the stack or inside other objects.

作为为什么继承是一个问题的(主要)示例,请考虑如果您允许结构具有具有更多成员的子类型,那么存储在低级别会受到怎样的影响。任何存储该结构类型的内容都将根据它最终包含的子类型占用可变数量的内存,这将是一个分配噩梦。给定类的对象在编译时将不再具有恒定的、已知的大小,对于任何方法调用的堆栈帧也是如此。对于在堆上分配了存储空间并且在堆栈上或其他对象内部具有对该存储空间的常量大小的引用的对象不会发生这种情况。

This is just an intuitive, high-level explanation - See comments and other answers for both expanded and more precise information.

这只是一个直观的、高层次的解释 - 有关扩展和更精确的信息,请参阅评论和其他答案。

回答by Darin Dimitrov

Because it is the way structs are represented in .NET. They are value types and value types don't have a method table pointer allowing inheritance.

因为这是 .NET 中结构的表示方式。它们是值类型,值类型没有允许继承的方法表指针。

回答by Robert Paulson

You may find the answers to SO Question Why are .NET value types sealed?relevant. In it, @logicnprefers to ECMA 335, which states:

您可能会找到 SO 问题的答案为什么 .NET 值类型是密封的?相关的。其中,@logicnp指的是ECMA 335,其中指出:

8.9.10 Value type inheritance

  • [...]
  • Will be sealed to avoid dealing with the complications of value slicing.
  • The more restrictive rules specified here allow for more efficient implementation without severely compromising functionality.

8.9.10 值类型继承

  • [...]
  • 将被密封以避免处理值切片的并发症。
  • 此处指定的更严格的规则允许更有效的实施,而不会严重影响功能。