简体   繁体   English

如何/为什么可能从引用类型派生值类型?

[英]How / Why possible a value type derives from a reference type?

In .NET , all value types inherit from the class named System.ValueType . .NET ,所有值类型都继承自名为System.ValueType的类。 System.ValueType is a class, so it is a reference type . System.ValueType是一个类,因此它是一个reference type

My question is how and why possible a value type derives from a reference type ? 我的问题是value type如何以及为什么可能从reference type派生?

The following is the key paragraph in the documentation 以下是文档中的关键段落

Although ValueType is the implicit base class for value types, you cannot create a class that inherits from ValueType directly. 尽管ValueType是值类型的隐式基类,但是您不能创建直接从ValueType继承的类。 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. 取而代之的是,各个编译器提供语言关键字或构造(例如C#中的struct和Visual Basic中的Structure…End Structure)来支持值类型的创建。

The inheritance occurs when the compiler compiles the overriden virtual methods of System.Object . 继承发生在编译器编译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#). System.ValueType类仅提供ToString()GetHashCode()等的更适当的重载。如文档所述,如果使用struct关键字(在C#中),则编译器将使用这些重载。 This tells the compiler use the System.ValueType methods instead of the System.Object methods. 这告诉编译器使用System.ValueType方法而不是System.Object方法。

It's possible because being a value type or reference type is not inherited. 可能是因为值类型或引用类型没有被继承。 The same applies to Enum . 枚举也是如此 The class itself is a reference type, but enums are value types. 类本身是引用类型,但枚举是值类型。

A possibly easier example is that all value types derive from System.Object , which is a reference type too. 一个可能更简单的示例是,所有值类型都派生自System.Object ,它也是一个引用类型。

int i = 3;
int j = 3;
object io = i;
object jo = j;

At this point, io and jo are references that refer to a copy of the values of i and j . 此时, iojo是引用ij值副本的引用。 The values can be extracted again using a cast: 可以使用强制转换再次提取值:

int i2 = (int)io;
int j2 = (int)jo;

Functionally, this works roughly as if the conversion to object creates a class ValueWrapper<T> { public T value; } 从功能上来说,这大致就像转换为object创建了class ValueWrapper<T> { public T value; } class ValueWrapper<T> { public T value; } object behind the scenes, and io is set to new ValueWrapper<int> { value = i } . class ValueWrapper<T> { public T value; } ,然后将io设置为new ValueWrapper<int> { value = i } The cast from io to int then reads ((ValueWrapper<int>)io).value . ioint然后读取((ValueWrapper<int>)io).value

This is not exactly what happens, but what does happen is similar enough that this hopefully clarifies sufficiently. 这不完全是发生的情况,但是发生的情况非常相似,因此希望可以充分澄清。

Eric Lippert says in The C# Programming Language 4th Edition : 埃里克·利珀特Eric Lippert )在《 C#编程语言第四版》中说

This point is frequently confusing to novices. 这一点经常使新手感到困惑。 I am often asked, “But how is it possible that a value type derives from a reference type?” I think the confusion arises as a result of a misunderstanding of what “derives from” means. 我经常被问到:“但是,值类型怎么可能从引用类型中派生出来呢?”我认为这种混淆是由于对“派生自”的含义的误解而引起的。 Derivation does not imply that the layout of the bits in memory of the base type is somewhere found in the layout of bits in the derived type. 派生并不意味着基本类型的内存中的位的布局在派生类型的位的布局中可以找到。 Rather, it simply implies that some mechanism exists whereby members of the base type may be accessed from the derived type. 相反,它仅表示存在某种机制,可以从派生类型访问基本类型的成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何测试Type是否从抽象基类泛型类型派生? - How to test if Type derives from an abstract base generic type? 如何使用受约束的泛型类型参数将 class 实例化为派生接口 - How to instantiate a class as the interface that it derives from with constrained generic type parameter 确定对象是否来自特定类型? - Determine if object is or derives from specific Type? 判断object是否派生自集合类型 - Determine if object derives from collection type 类型库导出程序遇到了从通用类派生的类型 - Type library exporter encountered a type that derives from a generic class 如何确定对象的类型是否实现了IEnumerable <X> 其中X使用Reflection从Base派生 - How to find out if an object's type implements IEnumerable<X> where X derives from Base using Reflection 如何强制一个类实现从特定基类/接口(而不是特定类型)派生的属性 - How to force a class to implement a property that derives from a specific base class/interface (rather than is of a specific type) 创建一个List实例,其类型派生自基类和接口 - Create an instance of a List where the type derives from a base class and an interface 出口工厂 - ExportFactory<T,TMetaData - Get type of class which derives from T 检查类型是否派生自具有多个通用参数的接口 - Check if a Type derives from a Interface with more than one Generic Argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM