简体   繁体   English

null == null返回false?

[英]null == null returning false?

Can anyone provide any ideas why my null check is evaluating incorrectly? 谁能提供任何想法为什么我的空检查评估不正确?

It should also be pointed out, that if i is not null, the evaluation is correct. 还应指出,如果i不为null,则评估是正确的。 I'm not sure what else is relevant, but let me know what other details I need to provide. 我不确定还有什么相关的,但请告诉我我需要提供的其他详细信息。

foreach (var item in selectedItems)
{
    var i = inspectionItems.Keys.SingleOrDefault(x => x.InspectionItemId == item.InspectionItemId);
    if (i != null)
    {
        // Process i
     }
     else
     {
       // create
     }
}

在此处输入图片说明

在此处输入图片说明

My guess would be that you overloaded the == operator. 我的猜测是您重载了==运算符。 The result of Object.Equals(i, null) could confirm that. Object.Equals(i, null)可以确认这一点。

If you are overloading the == operator, it's possible your logic is incorrectly handling the null case. 如果要重载==运算符,则可能是您的逻辑错误地处理了null情况。

One [devious] way to get this to happen would be :- 一种使这种情况发生的[曲折]方法是:-

    public enum Cheat
    {
        @null = 0,
        foo = 1,
        bar = 2
    }

If you assign Cheat.@null to i you get the watch values you show. 如果您将Cheat.@null分配给i则会得到显示的手表值。

在此处输入图片说明

What is the type of i ? i是什么类型? Either it displays in the debugger as null or you've done something else in that class that could cause == to fail. 它在调试器中显示为null或者您在该类中做了其他可能导致==失败的事情。

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

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