简体   繁体   中英

Null is not null?

Following code

        if (_LastMouseClickArea != null)
        {
            if (_LastMouseClickArea.GridItem == GridItem.DataCell)
            {...

fails because _LastMouseClickArea is null. I know it sounds silly, so I came here.

From Immediate Window:

_LastMouseClickArea != null
true
_LastMouseClickArea 
null
_LastMouseClickArea == null
false
_LastMouseClickArea.GetType()
'((object)(this._LastMouseClickArea))' is null

What could cause this behavior?

Is it possible you have a race condition? As in it's not null when you check, but then a thread sets it null before you are able to use it.

Resolution: Before submitting, I remembered to do some Rubber duck debugging. That led me to check how == is implemented, and, sure enough, there was some strange P/Invoke call for which null != null equals true .

So here it is, for future reference, if anyone encounters the same issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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