Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorthand for conditional statements

I am looking for a way to write something like this:

if (product.Category.PCATID != 10 && product.Category.PCATID != 11 && product.Category.PCATID != 16) {   }

In a shorthand way like below, which does not work of course:

if (product.Category.PCATID != 10 | 11 | 16) {   }

So is there shorthand way at all to do something similar ?

like image 442
LaserBeak Avatar asked Dec 03 '22 01:12

LaserBeak


1 Answers

Yes - you should use a set:

private static readonly HashSet<int> FooCategoryIds
    = new HashSet<int> { 10, 11, 16 };

...

if (!FooCategoryIds.Contains(product.Category.PCATID))
{
}

You can use a list or an array or basically any collection, of course - and for small sets of IDs it won't matter which you use... but I would personally use a HashSet to show that I really am only interested in the "set-ness", not the ordering.

like image 96
Jon Skeet Avatar answered Dec 17 '22 00:12

Jon Skeet