Arlen Beiler
Arlen Beiler

Reputation: 15876

How to check if something equals any of a list of values in C#

What is the OR operator in an IF statement

I asked this question way back when, and when I got the (very helpful) answer, I thought you should be able to say if (title == "User greeting" || "User name") {do stuff}. Now if it isn't obvious why that won't work, please refer to the other question and its accepted answer.

I am wondering, however, if there is a way to give an arbitrary list of strings to check if something equals any of them. Something like if(Array.OR(title, { "User greeting", "User name" })) continue; Is there such a thing or am I shooting in the dark? It seems like it'd be rather simple to implement.

Upvotes: 7

Views: 22325

Answers (4)

Deilan
Deilan

Reputation: 4886

More comprehensive and flexible solution honoring string comparisons:

using System;
using System.Collections.Generic;
using System.Linq;

public static class StringExtensions
{
    public static bool EqualsAny(this string target, StringComparer comparer, params string[] values)
    {
        return target.EqualsAny(comparer, (IEnumerable<string>) values);
    }

    public static bool EqualsAny(this string target, params string[] values)
    {
        return target.EqualsAny((IEnumerable<string>)values);
    }

    public static bool EqualsAny(this string target, StringComparer comparer, IEnumerable<string> values)
    {
        return values.Contains(target, comparer);
    }

    public static bool EqualsAny(this string target, IEnumerable<string> values)
    {
        return values.Contains(target, StringComparer.OrdinalIgnoreCase);
    }
}

Usage:

if (title.EqualsAny("User greeting", "User name"))
{
    //do stuff
}

P. S. Repeating the words of @Servy: Such solution works fine for a small set of values, for large sets consider using Dictionary, HashSet, etc.

Upvotes: 3

Arlen Beiler
Arlen Beiler

Reputation: 15876

CorrugatedAir's example is pretty good, however you can include it inline if needed.

if (new string[] { "test1", "test2", "test3" }.Contains("test1")) Console.WriteLine("it works");

And it does work: http://ideone.com/QzbvKV (Thanks Soner)

So my code would look like: if (new string[] { "User greeting", "User name" }.Contains(title)) Console.WriteLine("title contained");

http://ideone.com/PYugJu

P.S. Thanks Soner for the link, I never heard of ideone before!

Upvotes: 5

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98760

You can use Enumerable.Contains() method on this situation. Plus side of this method is enumeration is terminated as soon as a matching element is found.

Determines whether a sequence contains a specified element by using the default equality comparer.

string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };
string fruit = "mango";
bool hasMango = fruits.Contains(fruit);

Here is a DEMO.

Don't forget to add System.Linq namespace.

Upvotes: 1

CorrugatedAir
CorrugatedAir

Reputation: 819

You could try the Contains operator:

String[] array = {"One", "Two", "Three"};
if (array.Contains("One"))
{
     //Do stuff       
}

Upvotes: 11

Related Questions