C # syntax if (value = this || this || this) do it

just to throw away some code

string answer = "hotel"
if (answer == "house"|| answer == "hotel" || answer =="appartment")
{
    DoSomething()
}

I was wondering if there is a way to shorten it to

string answer = "hotel"
if (answer == "house"|| "hotel" || "appartment")
{
    DoSomething()
}

I know the switch statement

switch (answer)
{
    case "house":
    case "hotel":
    case "appartment": DoSomething();
        break;
    default :DoNothing();
}

I'm just wondering if there is any kind of syntactic sugar like what I described above.

+3
source share
10 answers

To do this, you can use some kind of syntactic sugar:

if((new[]{"house", "hotel", "apartment"}).Contains(answer))
{
}

Note that this will create a new array on the fly, so it will potentially be more expensive than just checking Boolean.

+8
source

You can use an array and use Contains.

So in your example:

string answer = "hotel";
string[] acceptable = new string[]{"house", "hotel", "appartment"};
if (acceptable.Contains(answer)){
    DoSomething();
}
+7
source
public static bool Any<T>(T item, params T[] items)
{
    return items.Contains(item);
}

:

if (Any(6, 1, 2, 3, 4, 5, 6, 7))
{
    // 6 == 6
}

if (Any("Hi", "a", "cad", "asdf", "hi"))
{
}
else
{
    // "Hi" != "hi" and from anything else.
}

:

public string[] items = new string[] {"a", "cad", "asdf", "hi"};

...

if (Any("Hi", items))
{
    Works just as well.
}

. , :

if (person.Name == p1.Name ||
    person.Name == p2.Name ||
    person.Name == p3.Name ||
    person.Name == p4.Name ||
    person.Name == p5.Name || ...)
{
}

:

public static bool Any<T>(T item, Func<T, T, bool> equalityChecker, params T[] items)
{
    return items.Any(x => equalityChecker(item, x));
}

:

if (Any(person, (per1, per2) => p1.Name == p2.Name, p1, p2, p3, p4, p5, ...)
{
}

, , , :

public static bool Any<T>(this T item, params T[] items)
{
    return items.Contains(item);
}

:

var b = 6.Any(4, 5, 6, 7); // true

"" .

+4

.

List<string> validAnswers = new List<string> {"house", "house1", "apartment"};

if (validAnswers.Contains(answer))
    DoSomething();

List , answer

+3

, , :

List<String> words = new List<String> { "house", "hotel", "appartment" };
String answer = "house";

if (words.Contains(answer))
{
   DoSomething();
}
+2

if(new string[]{"house","hotel","appartment"}.Contains(asnwer))
{
...
}

if(new List<string>(){"house","hotel","appartment"}.Any(x=>x == answer)
{
}

...

+1

, ...

string answer = "hotel"
if (answer.EqualsAny("house", "hotel", "appartment"))
{
    DoSomething()
}
// Extending the thought to another version
if (answer.EqualsAll("house", "hotel", "appartment"))
{
    DoSomething()
}

public static class Extensions
{
    public static bool EqualsAny(this string value, params string[] compareAgainst)
    {
        foreach (var element in compareAgainst)
        {
            if(value == element)
               return true;
        }
        return false;
    }
    public static bool EqualsAll(this string value, params string[] compareAgainst)
    {
        foreach (var element in compareAgainst)
        {
            if(value != element)
                return false;
        }
        return true;
    }
}
+1

, ENUM . Ref MSDN. .  

0

I usually use the switch statement in this case, but as mentioned in the several answers above, you can put the results in a list or array and test it that way.

0
source

I would use an extension method like this:

public static bool In<T>(this T item, params T[] items)
{
    return items.Contains(item);
}

Use it as follows:

if ("hotel".In("house", "hotel", "apartment"))
or
if (1.In(1,2))
0
source

All Articles