checking if value is one of a group
-
Hi. let's say i have the following code:
int x; ... // some code if ((x == 1) || (x == 5) || (x == 7) || (x == 9)) { ... /some code }
is there an elegant way to code it without repeating the x object? i thought about something like: if (x == (1 || 5 || 7 || 9)) Thanks -
Hi. let's say i have the following code:
int x; ... // some code if ((x == 1) || (x == 5) || (x == 7) || (x == 9)) { ... /some code }
is there an elegant way to code it without repeating the x object? i thought about something like: if (x == (1 || 5 || 7 || 9)) Thanks -
Hi. let's say i have the following code:
int x; ... // some code if ((x == 1) || (x == 5) || (x == 7) || (x == 9)) { ... /some code }
is there an elegant way to code it without repeating the x object? i thought about something like: if (x == (1 || 5 || 7 || 9)) ThanksHi, another possibility:
if (Array.IndexOf(new int[] { 1, 5, 7, 9 }, x) >= 0) {
//...
}If you store the array in some field it would be shorter (and more efficient when called often):
//somewhere in your class
private int[] _goodNumbers = new int[] { 1, 5, 7, 9 };//your method
if (Array.IndexOf(_goodNumbers, x) >= 0) {
//...
}Robert