不要值类型(整数,小数,布尔,等...)从Object继承?小数、布尔、整数、类型

2023-09-02 10:51:39 作者:怪我近视没看清人心

寻找澄清这...

我听说在.net'一切'从Object继承。我还听说,值类型和引用类型之间的区别是,引用类型的对象和值类型继承没有。

I've heard that 'everything' in .Net inherits from Object. I've also heard that the difference between value types and reference types is that reference types inherit from Object and value types do not.

我的理解是,一切是一个对象(继承自System.Object);但值类型和引用类型是简单地从彼此不同。值类型在堆栈上分配和引用类型得到一个指示器放置在指向上堆的地址堆栈。

My understanding was that everything was an object (inherits from System.Object); but value types and reference types were simply 'different' from one another. Value types are allocated on the stack and reference types get a 'pointer' placed on the Stack that points to an address on the Heap.

是它的要点?是什么让一个Integer值类型?这是在语言固有的东西?

Is that the gist of it? What makes an Integer a value type? That's something inherent in the language?

推荐答案

值类型,如的Int32 ,是结构。

在VS 2008的C#的帮助文件(因为我已经打开它)的结构:

From the VS 2008 C# help file (since I had it open) on structs:

一个结构不能从另一个结构或类继承,而且不能是类的基础。直接从System.ValueType,后者继承从System.Object所有结构继承。

A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object.