C# 比较运算符如何与 null int 一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15777745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:54:45  来源:igfitidea点击:

How does comparison operator works with null int?

c#nullable

提问by Ron5504

I am starting to learn nullable types and ran into following behavior.

我开始学习可为空类型并遇到以下行为。

While trying nullable int, i see comparison operator gives me unexpected result. For example, In my code below, The output i get is "both and 1 are equal". Note, it does not print "null" as well.

在尝试可空 int 时,我看到比较运算符给了我意想不到的结果。例如,在我下面的代码中,我得到的输出是"both and 1 are equal"。请注意,它也不会打印“null”。

int? a = null;
int? b = 1;

if (a < b)
    Console.WriteLine("{0} is bigger than {1}", b, a);
else if (a > b)
    Console.WriteLine("{0} is bigger than {1}", a, b);
else
    Console.WriteLine("both {0} and {1} are equal", a, b);

I was hoping any non-negative integer would be greater than null, Am i missing something here?

我希望任何非负整数都大于 null,我在这里遗漏了什么吗?

采纳答案by nkvu

According to MSDN- it's down the page in the "Operators" section:

根据MSDN- 它位于“操作员”部分的页面下方:

When you perform comparisons with nullable types, if the value of one of the nullable types is nulland the other is not, all comparisons evaluate to falseexcept for !=

当您与可空类型执行比较时,如果可空类型之一的值是null而另一种不是,则所有比较的计算结果false都为!=

So both a > band a < bevaluate to falsesince ais null...

所以两者a > ba < b评估为false因为a是空的......

回答by Parimal Raj

As MSDN says

正如 MSDN 所说

When you perform comparisons with nullable types, if the value of one of the nullable types is null and the other is not, all comparisons evaluate to false except for != (not equal). It is important not to assume that because a particular comparison returns false, the opposite case returns true. In the following example, 10 is not greater than, less than, nor equal to null. Only num1 != num2 evaluates to true.

当您与可空类型执行比较时,如果可空类型之一的值为 null 而另一个不是,则所有比较的计算结果都为 false,除了 !=(不等于)。重要的是不要假设因为特定的比较返回 false,相反的情况返回 true。在以下示例中,10 不大于、小于或等于 null。只有 num1 != num2 评估为真。

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
 */

回答by GDS

To summarise: any inequality comparison with null (>=, <, <=, >) returns falseeven if both operands are null. i.e.

总结一下:任何与 null ( >=, <, <=, >) 的不等式比较都会返回,false即使两个操作数都为 null。IE

null >  anyValue //false
null <= null     //false

Any equality or non-equality comparison with null (==, !=) works 'as expected'. i.e.

与 null ( ==, !=) 的任何相等或不相等比较都“按预期”工作。IE

null == null     //true
null != null     //false
null == nonNull  //false
null != nonNull  //true

回答by Gunnar Siréus

Comparing C# with SQL

比较 C# 和 SQL

C#: a=null and b=null => a==b => true

C#:a=null 和 b=null => a==b => true

SQL: a=null and b=null => a==b => false

SQL:a=null 和 b=null => a==b => false