如何检查 '==' 中的空值 没有无限递归的运算符重载?
以下将导致 == 运算符重载方法无限递归
Foo foo1 = null;
Foo foo2 = new Foo();
Assert.IsFalse(foo1 == foo2);
public static bool operator ==(Foo foo1, Foo foo2) {
if (foo1 == null) return foo2 == null;
return foo1.Equals(foo2);
}
如何检查空值?
The following will cause infinite recursion on the == operator overload method
Foo foo1 = null;
Foo foo2 = new Foo();
Assert.IsFalse(foo1 == foo2);
public static bool operator ==(Foo foo1, Foo foo2) {
if (foo1 == null) return foo2 == null;
return foo1.Equals(foo2);
}
How do I check for nulls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您可以尝试使用对象属性并捕获生成的 NullReferenceException。 如果您尝试的属性是从 Object 继承或重写的,那么这适用于任何类。
You can try to use an object property and catch the resulting NullReferenceException. If the property you try is inherited or overridden from Object, then this works for any class.
回复更多重写运算符如何与 null 进行比较,在此处重定向为重复的。
在这样做是为了支持值对象的情况下,我发现新的表示法很方便,并且希望确保只有一个地方进行比较。 还利用 Object.Equals(A, B) 简化了空检查。
这将重载 ==、!=、Equals 和 GetHashCode
对于更复杂的对象,请在 Equals 和更丰富的 GetHashCode 中添加额外的比较。
replying more to overriding operator how to compare to null that redirects here as a duplicate.
In the cases where this is being done to support Value Objects, I find the new notation to handy, and like to ensure there is only one place where the comparison is made. Also leveraging Object.Equals(A, B) simplifies the null checks.
This will overload ==, !=, Equals, and GetHashCode
For more complicated objects add additional comparisons in Equals and a richer GetHashCode.
查看此
参考 重载 Equals() 的指南和运算符 ==
check out this
reference Guidelines for Overloading Equals() and Operator ==
尝试
Object.ReferenceEquals(foo1, null)
无论如何,我不建议重载
==
运算符; 它应该用于比较引用,并使用Equals
进行“语义”比较。Try
Object.ReferenceEquals(foo1, null)
Anyway, I wouldn't recommend overloading the
==
operator; it should be used for comparing references, and useEquals
for "semantic" comparisons.在这种情况下,实际上有一种更简单的方法来检查
null
:就是这样!
此功能是在 C# 7 中引入的
There is actually a simpler way of checking against
null
in this case:That's it!
This feature was introduced in C# 7
如果我重写了
bool Equals(object obj)
并且我希望运算符==
和Foo.Equals(object obj)
返回相同的值值,我通常像这样实现!=
运算符:运算符
==
在为我完成所有空检查后最终会调用foo1.Equals( foo2)
我已经重写它来进行实际检查两者是否相等。If I have overridden
bool Equals(object obj)
and I want the operator==
andFoo.Equals(object obj)
to return the same value, I usually implement the!=
operator like this:The operator
==
will then after doing all the null checks for me end up callingfoo1.Equals(foo2)
that I have overridden to do the actual check if the two are equal.我的方法是依靠
object
自己的相等运算符来完成,这不会出错。 或者自定义扩展方法(和重载):
或者为了处理更多情况,可能是:
约束阻止值类型上的
IsNull
。 现在它就像调用一样甜蜜,这意味着我有一种一致/不易出错的风格来检查空值。 我还发现
(object)item == null
比Object 快得多。 ReferenceEquals(item, null)
,但前提是它很重要(我目前正在研究一些必须对所有内容进行微优化的事情!)。要查看有关实施相等性检查的完整指南,请参阅 比较引用类型的两个实例的“最佳实践”是什么?
My approach is to do
upon which I'm relying on
object
's own equality operator which can't go wrong. Or a custom extension method (and an overload):or to handle more cases, may be:
The constraint prevents
IsNull
on value types. Now its as sweet as callingwhich means I have one consistent/not-error-prone style of checking for nulls throughout. I also have found
(object)item == null
is very very very slightly faster thanObject.ReferenceEquals(item, null)
, but only if it matters (I'm currently working on something where I've to micro-optimize everything!).To see a complete guide on implementing equality checks, see What is "Best Practice" For Comparing Two Instances of a Reference Type?
对于现代和简洁的语法:
For a modern and condensed syntax:
static
Equals(Object, Object)
方法 指示两个对象objA
和objB
是否相等。 它还使您能够测试值为null
的对象是否相等。 它比较objA
和objB
是否相等:true
。 此测试相当于调用ReferenceEquals
方法。 此外,如果objA
和objB
均为null
,则该方法返回true
。objA
或objB
是否为null
。 如果是,则返回false
。如果两个对象不代表相同的对象引用,且都不为 null,则调用 objA.Equals(objB) 并返回结果。 这意味着如果
objA
重写Object.Equals(Object)
方法,则会调用此重写。。
The static
Equals(Object, Object)
method indicates whether two objects,objA
andobjB
, are equal. It also enables you to test objects whose value isnull
for equality. It comparesobjA
andobjB
for equality as follows:true
. This test is equivalent to calling theReferenceEquals
method. In addition, if bothobjA
andobjB
arenull
, the method returnstrue
.objA
orobjB
isnull
. If so, it returnsfalse
.If the two objects do not represent the same object reference and neither is
null
, it callsobjA.Equals(objB)
and returns the result. This means that ifobjA
overrides theObject.Equals(Object)
method, this override is called..
使用ReferenceEquals:
Use
ReferenceEquals
:在重载方法中强制转换为对象:
Cast to object in the overload method:
如果您使用 C# 7 或更高版本,则可以使用 null 常量模式匹配:
这为您提供了比调用 object.ReferenceEquals(foo1, null) 的代码稍微简洁的代码
If you are using C# 7 or later you can use null constant pattern matching:
This gives you slightly neater code than the one calling object.ReferenceEquals(foo1, null)
使用
ReferenceEquals< /代码>。 来自 MSDN 论坛:
Use
ReferenceEquals
. From the MSDN forums: