String comparion against a set of string values

.net, c#, string

Solution

There are many ways of doing it. One would be as follows:

var target = new HashSet<string>{ "abc", "efg", "lmn" };
if (target.Contains(x)) {
    ...
}

At max [my list of strings] can grow to 50 strings which is a rare possibility.

Then you should make `target` a `static readonly` in your class, like this:

private static readonly StringTargets = new HashSet<string>{ "abc", "efg", "lmn" };

Doing so would ensure that the set is created only once, and is not re-created each time the execution goes through the method that uses it.

Problem

I have a function like this(foo): I need to compare the input string and perform a task accordingly . Task is same, but only for a selected set of values. For all other values do nothing. ``` function foo(string x) { if(x == "abc") //do Task1 if(x == "efg") //do Task1 if(x == "hij") //do Task1 if(x == "lmn") //do Task1 } ``` Is there any other means to do checking other than this? Or putting `OR` operator inside `if`? What is the preferred way?

Original source

Related problems