如果 - 或语句的计算结果为TRUE,实际上它应该为FALSE

Ron*_*Ron -5 c# if-statement

不知道我在这里缺少什么..应该很简单..

tblCurrent不等于NULL tblCurrent.Rows.Count等于0

if (tblCurrent != null | tblCurrent.Rows.Count != 0)
{
    //Do something
}
else
{
    // This is what I want
}
Run Code Online (Sandbox Code Playgroud)

它应该看到正确的条件是0所以它应该返回false并放入else块?我错过了什么?

das*_*ght 5

如果 tblCurrent不相等null,则tblCurrent != null求值为true,所以整体OR也会评估true,因为OR评估true是否,并且仅当其中一方或两方评估时true.

看起来您的逻辑应该使用AND运算符&&而不是a OR,如下所示:

if (tblCurrent != null && tblCurrent.Rows.Count != 0) {
    ...
} else {
    ...
}
Run Code Online (Sandbox Code Playgroud)

该&&运营商短路的评价,所以你甚至不会当得到一个异常tblCurrent的null.