为什么使用 System.Decimal 继承层次结构
我的印象是所有值类型继承自System.ValueType,因为我知道 Decimal 是一个结构体,它也是一种值类型,也就是说 Decimal 因此必须是一个值类型。那么为什么 resharper 显示这样的类型层次结构:
还是我在这里误解了某些内容?
I was under the impression that all value types inherit from System.ValueType, and because I know that Decimal is a struct which is also a value type it goes to say that Decimal hence must be a value type. So why does resharper show the type hierarchy as such:
or am I misunderstanding something here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Decimal 并不是从 IFormattable 派生的,它只是实现了 IFormattable 接口。
实现接口有时被称为“继承”,它看起来几乎是一样的。
更令我惊讶的是 resharper 没有显示其余的接口。
VS 中的转到定义 (F12) 显示:
Decimal does not derive from IFormattable, it merely implements the IFormattable interface.
Implementing an interface is sometimes called 'inheriting', and it looks almost the same.
I'm more surprised resharper does not show the rest of the interfaces.
Go To definition (F12) in VS shows:
如果我在另一种视图模式(超类型层次结构)中显示(在 ReSharper 5.1 中)十进制,我会看到:
所以一切都如您所期望的那样。
If I show (in ReSharper 5.1) Decimal in another view mode (Supertypes Hierarchy) I see that:
So all is as you would expect.
在代码中表达的传统继承意义上,没有任何类型实际上继承自
System.ValueType
(有System.Enum
,但是就本次讨论而言,这并不重要)。ValueType
是一种特殊类型,不适合在代码中使用;您可以通过声明类型是struct
来“继承”它:这种类型的“继承”由编译器处理,编译器了解这些“特殊”类型(例如,还有
System.Void
)。您显示的继承层次结构对应于传统的继承概念,因此它不反映
ValueType
和Decimal
(或任何其他struct
)。There is no type that actually inherits from
System.ValueType
in the traditional sense of inheritance as expressed in code (there isSystem.Enum
, but for the purposes of this discussion it doesn't matter).ValueType
is a special type not intended to be used in code; you "inherit" from it by declaring that a type is astruct
:This type of "inheritance" is handled by the compiler, which has knowledge of these "special" types (for example, there's also
System.Void
).The inheritance hierarchy you show corresponds to the traditional concept of inheritance, so it does not reflect the relationship between
ValueType
andDecimal
(or any otherstruct
).