Jimmy
Jimmy

Reputation: 753

c# - is there a terser way to check if a variable is one of multiple things?

so currently I'm doing this:

if(variable == thing1 || variable == thing2 || variable == thing3)

But that's not super readable. What I'd like to do is something like this:

if(variable == thing1 || thing2 || thing3)

Does such a syntax exist in c#?

Upvotes: 3

Views: 1193

Answers (7)

Strider489
Strider489

Reputation: 67

This looks nice to me. If variable = 479 or 482 or 1482 or etc.

if (new int[] { 479, 482, 1482, 2760 }.Contains(variable))
{
    DoSomething();
}

Upvotes: 0

John Wu
John Wu

Reputation: 52280

You have a few options.

  1. Use switch (if thing1-thing3 are constant expressions)

    switch variable
        case thing1:
        case thing2:
        case thing3:
            DoSomething();
            break;
    
  2. Use a RegEx (only works for strings)

    if (RegEx.Match(variable, "^(thing1|thing2|thing3)"))
    {
        DoSomething();
    }
    
  3. Use an array

    string[] searchFor = new string[] {thing1, thing2, thing3};
    if (searchFor.Contains(variable))
    {
        DoSomething();
    }
    

Upvotes: 0

Douglas
Douglas

Reputation: 54897

If concise syntax is very important to you, you could define an extension method:

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

You could then use this like so:

if (variable.In(thing1, thing2, thing3))

That said, if the list being checked against will not change, I would prefer to declare it as a static readonly field, and call Contains against that. The above extension method may result in a new array being allocated each time it is called, which can hurt performance in tight loops.

private static readonly Thing[] _things = new [] { thing1, thing2, thing3 };

public void ProcessThing(Thing variable)
{
    if (_things.Contains(variable))
    {
        // ...
    }
}

Also, if the list being checked against contains more than a few items, use a HashSet<T> instead.

Upvotes: 9

Stefan
Stefan

Reputation: 17668

Put the test strings in an list or array and call Contains.:

var testers = new [] { "foo1", "foo2" };

if (testers.Contains("subject"))
{
   // test succeeded
} 

As an alternative:

if (new [] {"foo1", "foo2"}.Contains("subject"))
{
   // test succeeded
} 

Upvotes: 3

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 62002

Some people prefer an extension method:

public static bool IsOneOf<T>(this T self, params T[] values) => values.Contains(self);

or similar.

Then you can say:

if (variable.IsOneOf(thing1, thing2, thing3))

Oops, I see Douglas was first with this approach.

It uses the default equality comparer of T implicitly.

The disadvantage is that you create an extension method to all types. If you only need it for e.g. string, you may of course create a less general extension method.

Upvotes: 3

LoztInSpace
LoztInSpace

Reputation: 5697

If you put all your things into a collection of some sort then yes, you can use LINQ and Any

https://msdn.microsoft.com/en-us/library/system.linq.enumerable.any(v=vs.110).aspx

Upvotes: 1

user2887596
user2887596

Reputation: 643

You cound do:

int[] aux=new int[]{1,2,3}; 
if(Array.contains(aux, value))

Upvotes: 1

Related Questions