.NET 中如何表示 null

发布于 2024-07-11 03:11:56 字数 128 浏览 7 评论 0原文

我正在和一位同事谈话,谈到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦在深巷 2024-07-18 03:11:56

它是 0。

摘自 ECMA-335 规范中的 ldnull:

可能会认为 ldnull 是
冗余:为什么不使用 ldc.i4.0 或
ldc.i8.0 代替? 答案是
ldnull 提供了一个与大小无关的 null –
类似于 ldc.i 指令,
这是不存在的。 然而,即使
CIL 将包括一个 ldc.i
指导它仍然会受益
验证算法保留
ldnull 指令,因为它使
类型跟踪更容易。

It's 0.

Snip from ldnull in ECMA-335 spec:

It might be thought that ldnull is
redundant: why not use ldc.i4.0 or
ldc.i8.0 instead? The answer is that
ldnull provides a size-agnostic null –
analogous to an ldc.i instruction,
which does not exist. However, even if
CIL were to include an ldc.i
instruction it would still benefit
verification algorithms to retain the
ldnull instruction because it makes
type tracking easier.

无语# 2024-07-18 03:11:56

当可空类型实例设置为 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.

祁梦 2024-07-18 03:11:56

根据我的经验,我相信 Nothing 不会起作用,DBNull.Value 也会起作用

from my experience, I believe Nothing will work, and so will DBNull.Value

清泪尽 2024-07-18 03:11:56

来自 MSDN

null 关键字是一个文字,
代表一个空引用,即
不引用任何对象

为了在旧版本的 c# 中尝试使其更清楚一点,值类型不能为 null,即它必须有一个值,如果你没有分配一个值,你就会得到一个潜在的随机值,例如:

int i;
i++;
console.writeline(i);

在旧版本的 c# 中,对象必须初始化,否则它们为 null,这意味着它们没有对任何对象的引用。

现在,使用 c# 2.0+ 中的可空值类型,您可以拥有可空 int,这意味着如果您有以下代码:

int? i;
i++;
console.writeline(i);

您实际上会在 i++ 处得到异常,因为 i 从未被初始化为除 null 之外的任何值。 如果 null 为 0,则此代码可以正常执行,因为它的计算结果只是 0+1,但这是不正确的行为。

如果 null 始终为 0 并且您有一个可为空的 int,并且您编写了一些代码,例如:

int? i;
if (int == 0)
{
//do something
}

如果 null 与 0 相同,那么您很可能会得到一些意外的行为,因为编译器无法区分 int为 null 并且 int 被显式设置为 0。

另一个例子澄清了我的想法:

public int? AddNumbers(int? x, int? y)
{
    if (x == null || y == null)
        return null;
    if (x == 0)
        return y;
    if (y == 0)
        return x;

    return x + y;
}

在这个例子中,很明显 null 和 0 非常不同,因为如果你为 x 或 y 传递 0,并且 null 是相等的为 0,那么上面的代码永远不会检查 x == 0 或 y == 0,但是如果您运行代码并为 x 或 y 传入 0,则检查确实会被执行。

From MSDN:

The null keyword is a literal that
represents a null reference, one that
does not refer to any object

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:

int i;
i++;
console.writeline(i);

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:

int? i;
i++;
console.writeline(i);

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:

int? i;
if (int == 0)
{
//do something
}

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:

public int? AddNumbers(int? x, int? y)
{
    if (x == null || y == null)
        return null;
    if (x == 0)
        return y;
    if (y == 0)
        return x;

    return x + y;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文