1

Is there a more concise way to check if x is either "a","b","c","d" or "e"?

if (x == "a" | x == "b" | x == "c" | x == "d" | x == "e"){//do something}

Basically I want to know if I can express the same if statement without repeating the variable name x.

3 Answers 3

5

How about:

string[] whiteList = { "a", "b", "c", "d", "e" };

if(whiteList.Contains(x))
{
    ...
}

You can consider using a HashSet<string> or similar and caching a reference to it in a field for improved performance (to give you an O(1) Contains operation and to avoid allocating, populating the collection).

//Field
HashSet<string> _whiteList = new HashSet<string> { "a", "b", "c", "d", "e" };

....

// In a method:
if(_whiteList.Contains(x))
{
    ...
}
7
  • 1
    I think you could also just shortcut to new[]{"a","b"..."e"}.Contains(x) no?
    – jcolebrand
    Aug 11, 2012 at 7:22
  • Or rather, (new[]...).Contains ... idk, I don't have an IDE open to test with, and rather not load one just for this.
    – jcolebrand
    Aug 11, 2012 at 7:23
  • @jcolebrand: You could, but that would create a new copy of the array each time. This approach allows a single array to be created once and reused.
    – Jon Skeet
    Aug 11, 2012 at 7:23
  • @JonSkeet only if he caches it, right? As in, if he creates the whiteList var everytime this check is done, that would be the same as what I suggest?
    – jcolebrand
    Aug 11, 2012 at 7:24
  • @jcolebrand: Yes, in that case it would be the same.
    – Jon Skeet
    Aug 11, 2012 at 7:25
1

You can use Exists on array of "a-d".

new string[]{"a","b",...}.Exists(x)

6
  • @jcolebrand: just corrected, cause first I make an example on char array.
    – Tigran
    Aug 11, 2012 at 7:28
  • Do you still need the leading string? I don't think you do.
    – jcolebrand
    Aug 11, 2012 at 7:28
  • @jcolebrand: sure you need it. How you gonna create a string array so?
    – Tigran
    Aug 11, 2012 at 7:31
  • Thank you for your answer as well, Tigran.
    – IneedHelp
    Aug 11, 2012 at 7:40
  • 1
    @jcolebrand: ah ok I missunderstood your question.. In real code I usually skip it, but in examples tend to leave it, to clearly manifest my ideas.
    – Tigran
    Aug 11, 2012 at 10:47
0

Assuming you need to check only characters, comparing the ascii code of characters could do it as under.

if(x >= 'a' && x <= 'e')
{

}
7
  • What if x is a unicode character? what if it's not something that boxes to an int?
    – jcolebrand
    Aug 11, 2012 at 7:22
  • Before down voting this please explain to me how this answers the question Aug 11, 2012 at 7:23
  • I agree, I didn't think it was so bad as to need a downvote, but felt I should mention that ...
    – jcolebrand
    Aug 11, 2012 at 7:24
  • It is type casting the character to int and ascii code of a is 97 which code be used in condition
    – Adil
    Aug 11, 2012 at 7:26
  • 1
    I didn't down vote it, but this doesn't answer his question. He was pretty much looking for the Contains method. Aug 11, 2012 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.