C#如何将“||”与“!=”结合使用?

时间:2021-07-14 23:07:03

Why does adding an "||" OR between 2 "!=" not work for me?

为什么要添加“||”在2“!=”之间或者对我不起作用?

When 'name' is "test" or "test2" my if statement doesn't work if I've used 2 "!=" but if I use just one it does, please tell me why.

当'name'是“test”或“test2”时,如果我使用2“!=”我的if语句不起作用,但如果我只使用它,请告诉我原因。

if (col.Name != "test" || col.Name != "test2")
 {
  MessageBox.Show("No" + col.Name.ToString()); //This shows "No test" and "No test2"
 }
  else
 {
  MessageBox.Show("YES " + col.Name.ToString()); //does not reach here
 }

this works with no "||".

这适用于没有“||”。

if (col.Name != "test")
 {
  MessageBox.Show("No" + col.Name.ToString());
 }
  else
 {
  MessageBox.Show("YES " + col.Name.ToString()); //Shows "YES test"
 }

Thanks all

4 个解决方案

#1


17  

try this:

col.Name != "test" && col.Name != "test2"

think about it... "if the number is not 1, or the number is not 2" will always be true, since no number is both 1 and 2 to makes both halves false. Now extend this to strings.

想一想......“如果数字不是1,或数字不是2”将永远是真的,因为没有数字是1和2都使得两半都是假的。现在将其扩展为字符串。

#2


7  

It works, but it's not what you want.

它有效,但它不是你想要的。

col.Name != "test" || col.Name != "test2"

always returns true, since if col.Name is "test", it's not "test2", so you have "false || true" => true. If col.Name is "test2", you get "true || false". If it's anything else, it evaluates to "true || true".

总是返回true,因为如果col.Name是“test”,那么它不是“test2”,所以你有“false || true”=> true。如果col.Name是“test2”,则会得到“true || false”。如果是其他任何东西,它的评估结果为“true || true”。

I can't be sure exactly what you want to do, but you probably need an and (&&) between them.

我不能确定你想要做什么,但你可能需要和它们之间的(和&)。

#3


4  

You need to do a AND and not a OR :)

你需要做一个AND而不是OR :)

Pseudo code:

if string1 not equal test AND not equal test2 than do...

如果string1不等于测试并且不等于test2而不是...

Here is the version corrected :

这是更正的版本:

if (col.Name != "test" && col.Name != "test2")
{
  MessageBox.Show("No" + col.Name.ToString()); //This shows "No test" and "No test2"
}
else
{
  MessageBox.Show("YES " + col.Name.ToString()); //does not reach here
}

#4


4  

You're using OR, consider the truth table:

您正在使用OR,请考虑真值表:

p          q        p || q
true      true      true
true      false     true
false     true      true
false     false     false

You should use AND for the desired behavior...

你应该使用AND来实现所需的行为......

#1


17  

try this:

col.Name != "test" && col.Name != "test2"

think about it... "if the number is not 1, or the number is not 2" will always be true, since no number is both 1 and 2 to makes both halves false. Now extend this to strings.

想一想......“如果数字不是1,或数字不是2”将永远是真的,因为没有数字是1和2都使得两半都是假的。现在将其扩展为字符串。

#2


7  

It works, but it's not what you want.

它有效,但它不是你想要的。

col.Name != "test" || col.Name != "test2"

always returns true, since if col.Name is "test", it's not "test2", so you have "false || true" => true. If col.Name is "test2", you get "true || false". If it's anything else, it evaluates to "true || true".

总是返回true,因为如果col.Name是“test”,那么它不是“test2”,所以你有“false || true”=> true。如果col.Name是“test2”,则会得到“true || false”。如果是其他任何东西,它的评估结果为“true || true”。

I can't be sure exactly what you want to do, but you probably need an and (&&) between them.

我不能确定你想要做什么,但你可能需要和它们之间的(和&)。

#3


4  

You need to do a AND and not a OR :)

你需要做一个AND而不是OR :)

Pseudo code:

if string1 not equal test AND not equal test2 than do...

如果string1不等于测试并且不等于test2而不是...

Here is the version corrected :

这是更正的版本:

if (col.Name != "test" && col.Name != "test2")
{
  MessageBox.Show("No" + col.Name.ToString()); //This shows "No test" and "No test2"
}
else
{
  MessageBox.Show("YES " + col.Name.ToString()); //does not reach here
}

#4


4  

You're using OR, consider the truth table:

您正在使用OR,请考虑真值表:

p          q        p || q
true      true      true
true      false     true
false     true      true
false     false     false

You should use AND for the desired behavior...

你应该使用AND来实现所需的行为......