简体   繁体   English

C#中按位或的空值检查

[英]Bitwise Or of null check in C#

I was going through the IEqualityComparer Example here I don't understand the need for the second condition with Bitwise Or 我在这里浏览IEqualityComparer示例我不了解使用Bitwise Or进行第二个条件的需要

if (b2 == null && b1 == null)
   return true;
else if (b1 == null | b2 == null)
   return false;

Why is there a need for such a condition? 为什么需要这种条件?

The first condition returns true (equal) if both are null. 如果两个条件均为null,则第一个条件返回true(等于)。

When you get to the second condition it's already determined that they aren't both null (or else the first condition would have been true.) 当您达到第二个条件时,已经确定它们都不都是空的(否则第一个条件将为真。)

So in the second condition if either one is null then that means that only one of them is null. 因此,在第二种情况下,如果任何一个为null,则意味着其中只有一个为null。 If one is null and the other isn't then they aren't equal. 如果一个为null,另一个为null,则它们不相等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM