.NET 中如何表示 null
我正在和一位同事谈话,谈到了 null 的话题。 他告诉我,在 .NET 幕后,这只是一个非常小的数字。 我一直认为该对象只是没有指向堆上任何内存的指针,但我不确定哪种方式。
所以我希望社区能为我们解决这个问题;P
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.
So i'm hoping the community can clear it up for us ;P
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它是 0。
摘自 ECMA-335 规范中的 ldnull:
It's 0.
Snip from ldnull in ECMA-335 spec:
当可空类型实例设置为 null 时,其基础值为零。
更具体地说,可空类型是一种将基础类型的值与布尔空指示符组合在一起的结构。 可为 null 类型的实例有两个公共只读属性:bool 类型的 HasValue 和可为 null 类型的基础类型的 Value。
HasValue 对于非空实例为 true,对于空实例为 false。
因此,当 HasValue 为 true 时,Value 属性将返回所包含的值。 当 HasValue 为 false 时,尝试访问 Value 属性会引发异常。 但如果你可以访问它,你会发现它包含零。
When a nullable type instance is set to null, its underlying value is zero.
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.
HasValue is true for a non-null instance and false for a null instance.
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.
根据我的经验,我相信
Nothing
不会起作用,DBNull.Value
也会起作用from my experience, I believe
Nothing
will work, and so willDBNull.Value
来自 MSDN:
为了在旧版本的 c# 中尝试使其更清楚一点,值类型不能为 null,即它必须有一个值,如果你没有分配一个值,你就会得到一个潜在的随机值,例如:
在旧版本的 c# 中,对象必须初始化,否则它们为 null,这意味着它们没有对任何对象的引用。
现在,使用 c# 2.0+ 中的可空值类型,您可以拥有可空 int,这意味着如果您有以下代码:
您实际上会在 i++ 处得到异常,因为 i 从未被初始化为除 null 之外的任何值。 如果 null 为 0,则此代码可以正常执行,因为它的计算结果只是 0+1,但这是不正确的行为。
如果 null 始终为 0 并且您有一个可为空的 int,并且您编写了一些代码,例如:
如果 null 与 0 相同,那么您很可能会得到一些意外的行为,因为编译器无法区分 int为 null 并且 int 被显式设置为 0。
另一个例子澄清了我的想法:
在这个例子中,很明显 null 和 0 非常不同,因为如果你为 x 或 y 传递 0,并且 null 是相等的为 0,那么上面的代码永远不会检查 x == 0 或 y == 0,但是如果您运行代码并为 x 或 y 传入 0,则检查确实会被执行。
From MSDN:
To try and make it a little clearer in the older versions of c#, a value type couldn't be null, ie, it HAD to have a value, if you didn't assign one, you got a potentially random value so something like:
in the older versions of c# objects had to be initialized otherwise they were null, which meant they had no reference to any object.
Now with nullable value types in c# 2.0+ you can have a nullable int which means that if you have this code:
you will actually get an exception at i++ because i has never been initialized to anything other than null. If null was 0, this code would execute fine because it'd just evaluate to 0+1, however this is incorrect behaviour.
If null was always 0 and you had a a nullable int, and you wrote some code like:
there is a very real possibility you could get some unexpected behaviour IF null was the same as 0 because there is no way the compiler could differentiate between the int being null and the int being explicitly set to 0.
Another example that clarifies things in my mind:
in this example, it's clear that null and 0 are very different because if you were to pass in 0 for x or y, and null was equal to 0, then the code above would never get to the checks for x == 0 or y == 0, however if you run the code and pass in 0 for x or y, the checks do get executed.