If int is equal to multiple numbers

Hey so at the moment i have my script like this

if (index == 0 || index == 2 || index == 4 || index == 6 || index == 8 || index == 10 || index == 12 || index == 13 || index == 15 || index == 17 || index == 18)

But im wondering if there’s an easier way to do it so i dont have ti right out the index == part each time

You could make a collection of numbers and see if your int is contained in that collection:

HashSet<int> numbers = new HashSet<int>() { 2, 4, 6, 8, 10, 12, 13, 15, 17, 18 };

void MyCode() {
  if (numbers.Contains(index)) {
    // index is in the collection.
  }
}
2 Likes

You can put those numbers into a HashSet<int> (once) and then queries are going to be extremely fast and look like:

if (NumberSet.Contains(i))
{
  /// yooop!
}
1 Like

And if you didn’t already have 13, 15, and 17 thrown in the mix, you could’ve done it like this

if((index & 1) == 0) { // checks for even integers
 // yipp
}

But alas

1 Like