当 == 对于 null 值返回 true 时,为什么 >= 返回 false?

发布于 2024-10-07 00:36:23 字数 182 浏览 6 评论 0原文

我有两个int类型的变量? (或者 Nullable如果你愿意的话)。我想对两个变量进行大于或等于 (>=) 比较,但事实证明,如果两个变量都为 null,则返回 false,而显然 == 运算符返回 true。

有人可以向我解释为什么这是合乎逻辑的,因为 >= 运算符的语义定义包含单词“or”?

I have two variables of type int? (or Nullable<int> if you will). I wanted to do a greater-than-or-equal (>=) comparison on the two variables but as it turns out, this returns false if both variables are null, while obviously the == operator returns true.

Can someone explain to me why that is logical because the semantical definition of the >= operator contains the word "or"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

╰◇生如夏花灿烂 2024-10-14 00:36:24

>= 仅在以特定的明确定义方式使用时表示“大于或等于”。当用于具有重载运算符的类时,它意味着类开发人员希望它意味着的任何内容。当应用于类似字符串的类时,它可能意味着“排序相同或更高”,或者可能意味着“相同长度或更长”。

>= only means "greater than or equal" when used in that specific well defined way. When used on a class with overloaded operators it means anything the class developer wants it to mean. When applied to a string-like class, it might mean "sorts the same or higher" or it might mean "the same length or longer".

娇女薄笑 2024-10-14 00:36:24

由于默认情况下 int 不能为 null 并且其值将设置为 0,因此 > 运算符且<它是为 int 类型构建的,期望与 values 一起使用,而不是与 nulls 一起使用。

请参阅我对类似问题的回答,其中我编写了一些使用 less <greater > 运算符处理 nullable int 的方法 https://stackoverflow.com/a/51507612/7003760

Since by default an int cannot be null and its value will be set to 0, the operator of > and < which is built for int type, expects to work with values and not with nulls.

see my answer to a similar question where I wrote some ways to handle nullable int with the less < and greater > operators https://stackoverflow.com/a/51507612/7003760

桃扇骨 2024-10-14 00:36:24

NULL 不是零(数字或二进制值)、零长度字符串或空白(字符值)。
所以任何比较运算符总是返回 false。
在此处了解更多信息

NULL is not zero (numeric or binary value), a zero-length string, or blank (character value).
So any comparison operator will always return false on it.
Read more about it here

冷夜 2024-10-14 00:36:23

当该功能最初在 C# 2.0 中设计时,关于这一奇怪现象存在着巨大的争论。问题是 C# 用户已经完全习惯了这种意义:

if(someReference == null)

将相等性扩展到可为 null 值类型时,您有以下选择。

  1. 可空相等性确实得到了提升。。如果一个或两个操作数都为 null,则结果既不是 true,也不是 false,而是 null。在这种情况下,您可以:

    • a) 将 if 语句中的可空值类型相等设置为非法,因为 if 语句需要 bool,而不是可为 null 的 bool。相反,如果每个人想要与 null 进行比较,则要求他们使用 HasValue。这是冗长且令人恼火的。

    • b) 自动将 null 转换为 false。这样做的缺点是,如果 x 为 null,则 x==null 返回 false,这会令人困惑,并且不利于人们对 null 与引用类型比较的理解。

  2. 可空相等性不会被解除。可空相等要么为 true,要么为 false,与 null 的比较是空检查。这使得可为空的相等性与可为空的不等式不一致。

这些选择显然都不正确;它们都有优点和缺点。例如,VBScript 选择 1b。经过多次争论,C# 设计团队选择了#2。

There was a huge debate about this oddity when the feature was originally designed back in C# 2.0. The problem is that C# users are completely used to this being meaningful:

if(someReference == null)

When extending equality to nullable value types, you have the following choices.

  1. Nullable equality is truly lifted. If one or both of the operands is null then the result is neither true, nor false, but null. In this case you can either:

    • a) Make it illegal to have a nullable value type equality in an if statement, because the if statement needs a bool, not a nullable bool. Instead, require everyone to use HasValue if they want to compare to null. This is verbose and irritating.

    • b) Automatically convert null to false. The downside of this is that x==null returns false if x is null, which is confusing and works against people's understanding of null comparisons with reference types.

  2. Nullable equality is not lifted. Nullable equality is either true or false, and comparison to null is a null check. This makes nullable equality inconsistent with nullable inequality.

None of these choices is obviously correct; they all have pros and cons. VBScript chooses 1b, for example. After much debate the C# design team chose #2.

暮光沉寂 2024-10-14 00:36:23

因为平等是与可比性分开定义的。
您可以测试 x == nullx > null 是没有意义的。在 C# 中它总是错误的。

Because Equality is defined separately from Comparability.
You can test x == null but x > null is meaningless. In C# it will always be false.

违心° 2024-10-14 00:36:23

描述“>=”的另一种方式是:不少于。没有提到平等。一旦非相等测试中的操作数之一为 Null,结果也未知(为 null)。但是,如果您想知道两个操作数是否都为 Null,则 Null == Null 是一个合理的测试(应该结果为 true)。去掉运算符的不等式部分会带来很大的不同。

以下代码示例来自 http://msdn.microsoft.com/en- us/library/2cf62fcy.aspx#sectionToggle4 总结了 C# 如何处理 Null:

int? num1 = 10;   
int? num2 = null;   
if (num1 >= num2)   
{   
    Console.WriteLine("num1 is greater than or equal to num2");   
}   
else   
{   
    // This clause is selected, but num1 is not less than num2.   
    Console.WriteLine("num1 >= num2 returned false (but num1 < num2 also is false)");   
}   

if (num1 < num2)   
{   
    Console.WriteLine("num1 is less than num2");   
}   
else   
{   
    // The else clause is selected again, but num1 is not greater than   
    // or equal to num2.   
    Console.WriteLine("num1 < num2 returned false (but num1 >= num2 also is false)");   
}   

if (num1 != num2)   
{   
    // This comparison is true, num1 and num2 are not equal.   
    Console.WriteLine("Finally, num1 != num2 returns true!");   
}   

// Change the value of num1, so that both num1 and num2 are null.   
num1 = null;   
if (num1 == num2)   
{   
    // The equality comparison returns true when both operands are null.   
    Console.WriteLine("num1 == num2 returns true when the value of each is null");   
}   

/* Output:   
 * num1 >= num2 returned false (but num1 < num2 also is false)   
 * num1 < num2 returned false (but num1 >= num2 also is false)   
 * Finally, num1 != num2 returns true!   
 * num1 == num2 returns true when the value of each is null   
 */   

Another way of describing '>=' is: Not Less Than. No mention of equals. As soon as one of the operands in a non-equality test is Null, the result is unknown as well (is null). However if you want to know if both operands are Null, then Null == Null is a reasonable test (should result in true). Getting rid of the inequality part of the operator makes all the difference.

The following code example from http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx#sectionToggle4 summarizes how C# treats Null:

int? num1 = 10;   
int? num2 = null;   
if (num1 >= num2)   
{   
    Console.WriteLine("num1 is greater than or equal to num2");   
}   
else   
{   
    // This clause is selected, but num1 is not less than num2.   
    Console.WriteLine("num1 >= num2 returned false (but num1 < num2 also is false)");   
}   

if (num1 < num2)   
{   
    Console.WriteLine("num1 is less than num2");   
}   
else   
{   
    // The else clause is selected again, but num1 is not greater than   
    // or equal to num2.   
    Console.WriteLine("num1 < num2 returned false (but num1 >= num2 also is false)");   
}   

if (num1 != num2)   
{   
    // This comparison is true, num1 and num2 are not equal.   
    Console.WriteLine("Finally, num1 != num2 returns true!");   
}   

// Change the value of num1, so that both num1 and num2 are null.   
num1 = null;   
if (num1 == num2)   
{   
    // The equality comparison returns true when both operands are null.   
    Console.WriteLine("num1 == num2 returns true when the value of each is null");   
}   

/* Output:   
 * num1 >= num2 returned false (but num1 < num2 also is false)   
 * num1 < num2 returned false (but num1 >= num2 also is false)   
 * Finally, num1 != num2 returns true!   
 * num1 == num2 returns true when the value of each is null   
 */   
深居我梦 2024-10-14 00:36:23

>= 对数值进行操作;哪个 null 不是。

您可以重载 >= 运算符提供您想要的特定类型。

>= operates on a numeric value; which null is not.

You could overload the >= operator to provide what you desire on a specific type.

悟红尘 2024-10-14 00:36:23

您期望什么值?

null == null tr​​ue

null >= null false

null >= null null false

null <= null false

null < null false

null != null false

1 == null false 1

>= null false

1 >空假

1 <= 空假

1 <= null false

1 != null tr​​ue 又名 !(1 == null)

What values would you expect?

null == null true

null >= null false

null > null false

null <= null false

null < null false

null != null false

1 == null false

1 >= null false

1 > null false

1 <= null false

1 < null false

1 != null true aka !(1 == null)

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