[英]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
方法。
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
. 此时, io
和jo
是引用i
和j
值副本的引用。 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
. 从io
到int
然后读取((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.