bairdmar bairdmar - 4 months ago 29
C# Question

C# If not (this) Or (this) or (this) - Multiple condition IF/OR

I have a condition like:

if (!(InRange1 || InRange2 || InRange3))
//Do Something

Each of the InRange Variables are boolean.

The desired behavior is if anyone of those values is False I want to do something, but it is not working that way. All of the values have to be false before the event is triggered.

Does it need to be written as

if (!InRange1 || !InRange2 || !InRange3)
//do something

And, in either case I'm curious why the original statement doesn't work.


You can use DeMorgan's Law for this.

What you have is equivalent to NOR. !(A | B | C) is equivalent to !A & !B & !C by DeMorgan's Law.

What you want is NAND, so !(A && B && C). This is exactly equivalent to what you're looking for - !A | !B | !C.