From your comment
Q: in what way does it fail? – dutzu
A: The cat is allowed ._.
i assume that you want to keap away cats and ducks.
You condition fails because of the Logical OR Operator ||
. The second condition gets evaluated because "cat" != "cat"
returns false but "cat" != "duck"
returns true. That's why cats are also allowed to enter.
You probably want to stop both from entering with
if (varFoo != "cat" && varFoo != "duck")
Console.WriteLine("You can enter.");
else
Console.WriteLine("Not allowed.");
Update You last edit supports my opinion:
Just wanted something as
If(Either cat or a Duck)
// You're not allowed
Understand it differently: You define an action, this is to prevent some animals from entering. You want to apply this action on cats
and ducks
. You need an AND
instead
of an OR
. (see above code)
Another approach which does the same is to define a collection of forbidden animals and use Contains
:
IEnumerable<string> forbiddenAnimals = new List<string>(){ "cat", "duck" };
if(forbiddenAnimals.Contains(varFoo))
// You're not allowed
else
// you are welcomed