当 == 对于 null 值返回 true 时,为什么 >= 返回 false?
我有两个int类型的变量? (或者 Nullable
有人可以向我解释为什么这是合乎逻辑的,因为 >= 运算符的语义定义包含单词“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
>= 仅在以特定的明确定义方式使用时表示“大于或等于”。当用于具有重载运算符的类时,它意味着类开发人员希望它意味着的任何内容。当应用于类似字符串的类时,它可能意味着“排序相同或更高”,或者可能意味着“相同长度或更长”。
>= 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".
由于默认情况下
int
不能为 null 并且其值将设置为0
,因此 > 运算符且<它是为int
类型构建的,期望与values
一起使用,而不是与nulls
一起使用。请参阅我对类似问题的回答,其中我编写了一些使用
less <
和greater >
运算符处理nullable int
的方法 https://stackoverflow.com/a/51507612/7003760Since by default an
int
cannot be null and its value will be set to0
, the operator of > and < which is built forint
type, expects to work withvalues
and not withnulls
.see my answer to a similar question where I wrote some ways to handle
nullable int
with theless <
andgreater >
operators https://stackoverflow.com/a/51507612/7003760NULL 不是零(数字或二进制值)、零长度字符串或空白(字符值)。
所以任何比较运算符总是返回 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
当该功能最初在 C# 2.0 中设计时,关于这一奇怪现象存在着巨大的争论。问题是 C# 用户已经完全习惯了这种意义:
将相等性扩展到可为 null 值类型时,您有以下选择。
可空相等性确实得到了提升。。如果一个或两个操作数都为 null,则结果既不是 true,也不是 false,而是 null。在这种情况下,您可以:
a) 将
if
语句中的可空值类型相等设置为非法,因为if
语句需要 bool,而不是可为 null 的 bool。相反,如果每个人想要与 null 进行比较,则要求他们使用HasValue
。这是冗长且令人恼火的。b) 自动将 null 转换为 false。这样做的缺点是,如果 x 为 null,则
x==null
返回 false,这会令人困惑,并且不利于人们对 null 与引用类型比较的理解。可空相等性不会被解除。可空相等要么为 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:
When extending equality to nullable value types, you have the following choices.
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 theif
statement needs a bool, not a nullable bool. Instead, require everyone to useHasValue
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.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.
因为平等是与可比性分开定义的。
您可以测试
x == null
但x > null
是没有意义的。在 C# 中它总是错误的。Because Equality is defined separately from Comparability.
You can test
x == null
butx > null
is meaningless. In C# it will always be false.描述“>=”的另一种方式是:不少于。没有提到平等。一旦非相等测试中的操作数之一为 Null,结果也未知(为 null)。但是,如果您想知道两个操作数是否都为 Null,则 Null == Null 是一个合理的测试(应该结果为 true)。去掉运算符的不等式部分会带来很大的不同。
以下代码示例来自 http://msdn.microsoft.com/en- us/library/2cf62fcy.aspx#sectionToggle4 总结了 C# 如何处理 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:
>=
对数值进行操作;哪个 null 不是。您可以重载
>=
运算符提供您想要的特定类型。>=
operates on a numeric value; which null is not.You could overload the
>=
operator to provide what you desire on a specific type.您期望什么值?
null == null true
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 true 又名 !(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)