如何/为什么可能值类型从引用类型派生? [英] How / Why possible a value type derives from a reference type?

查看:152
本文介绍了如何/为什么可能值类型从引用类型派生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET ,所有的值类型的名为类继承的 System.ValueType System.ValueType 是一个类,所以它是一个引用类型

In .NET, all value types inherit from the class named System.ValueType. System.ValueType is a class, so it is a reference type.

我的问题是如何以及为什么可以从引用类型的值类型导出

My question is how and why possible a value type derives from a reference type?

推荐答案

以下是文档中的关键段落

The following is the key paragraph in the documentation

虽然值类型是值类型的隐含的基类,   不能创建一个类,直接继承自ValueType。代替,   个别编译器提供了一种语言的关键字或构建(如   结构在C#和结构......在Visual Basic中端结构)支持   价值创造型的。

Although ValueType is the implicit base class for value types, you cannot create a class that inherits from ValueType directly. Instead, individual compilers provide a language keyword or construct (such as struct in C# and Structure…End Structure in Visual Basic) to support the creation of value types.

在编译器编译 System.Object的的被覆盖的虚拟方法时发生继承。该 System.ValueType 类只是提供了更合适的重载的ToString() GetHash code()等。由于文件指出编译器使用这些重载如果结构关键字用于(在C#)。这告诉编译器使用 System.ValueType 方法,而不是 System.Object的的方法。

The inheritance occurs when the compiler compiles the overriden virtual methods of System.Object. The System.ValueType class simply provides more appropriate overloads of ToString(), GetHashCode() etc. As the document states the compiler uses these overloads if the struct keyword is used (in C#). This tells the compiler use the System.ValueType methods instead of the System.Object methods.

这篇关于如何/为什么可能值类型从引用类型派生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆