0

Let's say I have an if that goes like this:

if(condition1 != asd && menu != menu1 && menu != menu2)

Can this be shortened to something like:

if(condition1 != asd && menu != (menu1 && menu2))

I tried putting parentheses around the second condition either around the two that it should not be, and around the whole thing but it does not pass the compiler.

Is it possible to shorten the second condition this way? Or is there a better way to do it?

PS. I had no idea what to call this, so feel free to edit the title.

Esa
  • 1,636
  • 3
  • 19
  • 23

7 Answers7

5

Imagine if you were comparing numbers:

if (myNumber != 5 && myNumber != 10)

You could add the numbers being compared to a list first, then use a .Contains():

var forbiddenNumbers = new List<int> { 5, 10 };

if (!forbiddenNumbers.Contains(myNumber))

I don't see any reason you couldn't do the same with your menus, as you're simply comparing object references. (I'm not sure what the type is here, so I simply chose "Menu".)

var menusToCheck = new List<Menu> { menu1, menu2 };

if (!allowedMenus.Contains(menu))
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
1

You can use this extension-method:

public static class EqualsAnyExtension
{
    public static bool EqualsAny<T>(this T value, params T[] items)
    {
        return items.Contains(value);
    }
}

Use it like this:

if (condition1 != asd && !menu.EqualsAny(menu1, menu2))
lightbricko
  • 2,649
  • 15
  • 21
0

No, you can't do this like the second statement. Because

menu != (menu1 && menu2)

Would mean "compare menu with boolean AND operation on menu1 and menu2". While

menu != menu1 && menu != menu2

means "apply AND operation to results of comparison operation on menu and menu1 and comparison operation on menu and menu2".

ttaaoossuuuu
  • 7,786
  • 3
  • 28
  • 58
0

The logical operators yield boolean results. Therefore:

(menu1 && menu2)

evaulates to true or false depending on what the values of the variables are.

If menu1 or menu2 are not boolean then you'll need to do a comparison to yield a boolean, as you did in your first go:

if(condition1 != asd && menu != menu1 && menu != menu2)
Sean
  • 60,939
  • 11
  • 97
  • 136
0

You have not specified the data type of menu and the answer could depend on that.

For example if menu,menu1 and menu2 are all boolean then this could make some logical sense

if(menu != (menu1 && menu2))

(amounts to if menu1, menu2 and menu are all the same boolean value)

However if they are strings (or in fact anything other than booleans), then it does not make sense. You could still shorten it, but I wouldn't recommend it for 2 items:

var menuItems = new string[]{menu1,menu2}
if(condition1 != asd &&  menuItems.Contains(menu))
{
   ...
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0
 (menu1 && menu2)

in

 if(condition1 != asd && menu != (menu1 && menu2))

both menu1 and menu2 need to be of bool type otherwise it throws

CS0019: Operator '&&' cannot be applied to operands of type

I think it cannot be shortened anymore unless all of them are boolean values, when you can use XOR operator between menu1 and menu2.

Alok
  • 1,290
  • 1
  • 11
  • 21
0
var menusToCheck = new List<Menu> {"abc","def","eee","sdds" };
if (!menusToCheck .Contains(menu))
{
 ..spin..
}

Like this.

Hiren Raiyani
  • 754
  • 2
  • 12
  • 28