lundi 20 juillet 2020

Better way to write complex statement

Imagine you have int n and you have to make sure it isn't equal to certain numbers.

I would solve that like this:

int n = 5;
if (n != 4 && n != 11 && n != 1)
    do something..

The solution I'm looking for does it with enums, so:

int n = MyEnum.ValueThree;
if (n != MyEnum.ValueFour && n != MyEnum.ValueOne && n != MyEnum.ValueTen)
    do something..

There must some more performant or prettier way to write this.

Aucun commentaire:

Enregistrer un commentaire