-5

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

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

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

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

4

2 回答 2

5

If tblCurrent does not equal null, then tblCurrent != null evaluates to true, so the overall OR would evaluate to true as well, because an OR evaluates to true if, and only if, one or both of its sides evaluate to true.

It looks like your logic should have used the AND operator && instead of an OR, like this:

if (tblCurrent != null && tblCurrent.Rows.Count != 0) {
    ...
} else {
    ...
}

The && operator short-circuits the evaluation, so you would not get an exception even when the tblCurrent is null.

于 2013-09-05T18:30:10.403 回答
3

The correct OR operator is ||. The | operator is a bitwise OR.

Your logic requires AND, not OR.

if (tblCurrent != null && tblCurrent.Rows.Count != 0)
于 2013-09-05T18:29:06.850 回答