如何空重presented在.NETpresented、NET

2023-09-04 00:56:54 作者:EASON

我在和一个同事和空的题目的谈话上来。他告诉我,在幕后.NET,这只是一个非常小的数目。我一直以为,对象只是没有一个指向堆上的任何记忆,但我不知道任何一种方式。

I was having a conversation with a co-worker and the subject of null came up. He was telling me that in .NET behind the scenes it is just a really small number. I always thought that the object just didn't have a pointer to any memory on the heap but i wasn't sure either way.

所以我希望社会能够清除它为我们; P

So i'm hoping the community can clear it up for us ;P

推荐答案

在可空类型实例被设置为null,它的基本价值是零。

When a nullable type instance is set to null, its underlying value is zero.

要更具体,可空类型是结合了基础类型的值连同布尔空值指标的结构。可空类型的实例具有两个公共只读属性: hasVa​​lue的 bool类型的,和值可空类型的基础类型。

To be more specific, a nullable type is a structure that combines a value of the underlying type together with a boolean null indicator. An instance of a nullable type has two public read-only properties: HasValue of type bool, and Value of the nullable type’s underlying type.

hasVa​​lue的是真正的一个非空实例和虚假的空实例。

HasValue is true for a non-null instance and false for a null instance.

所以,当 hasVa​​lue的是真实的,在值属性返回所包含的值。当 hasVa​​lue的是假的,尝试访问Value属性将引发异常。但是,如果你能访问它,你会发现它包含为零。

So when HasValue is true, the Value property returns the contained value. When HasValue is false, an attempt to access the Value property throws an exception. But if you could access it, you would find it contains zero.