[英]Null is not null?
Following code 以下代码
if (_LastMouseClickArea != null)
{
if (_LastMouseClickArea.GridItem == GridItem.DataCell)
{...
fails because _LastMouseClickArea
is null. 失败,因为
_LastMouseClickArea
为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.
因为在检查时它不是null,但是在你能够使用它之前,线程将它设置为null。
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
. 这导致我检查如何实现
==
,当然,有一些奇怪的P / Invoke调用,其中null != null
等于true
。
So here it is, for future reference, if anyone encounters the same issue. 所以在这里,如果有人遇到同样的问题,将来参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.