Value is in enum list

39,454

Solution 1

Here is an extension method that helps a lot in a lot of circumstances.

public static class Ext
{
    public static bool In<T>(this T val, params T[] values) where T : struct
    {
        return values.Contains(val);
    }
}

Usage:

Console.WriteLine(1.In(2, 1, 3));
Console.WriteLine(1.In(2, 3));
Console.WriteLine(UserStatus.Active.In(UserStatus.Removed, UserStatus.Banned));

Solution 2

If it is a longer list of enums, you can use:

var allowed = new List<UserStatus> { UserStatus.Unverified, UserStatus.Active };
bool ok = allowed.Contains(status);

Otherwise there is no way around the long || predicate, checking for each allowed value.

Solution 3

Use Enum.IsDefined

example:

public enum enStage {Work, Payment, Record, Return, Reject};
int StageValue = 4;

Enum.IsDefined(typeof(enStage), StageValue)

Solution 4

You can do this in .NET 4.0+ using Enum.HasFlag(Enum) method,

UserStatus status = UserStatus.Unverified; // just assumed status is Unverified

bool ok = (UserStatus.Unverified | UserStatus.Active).HasFlag(status);

You can also do the same by assigning into a variable like,

UserStatus status = UserStatus.Active; // just assumed status is Active

UserStatus unverifiedOrActive = UserStatus.Unverified | UserStatus.Active;

bool ok = unverifiedOrActive.HasFlag(status);

Solution 5

UserStatus userStatus = null;
Eum.TryParse<UserStatus>(status.ToString(), out userStatus);

if(userStatus != null)
{
  //it is not in the list
}
Share:
39,454
Jerad Rose
Author by

Jerad Rose

C#, MVC, and React web developer. Developer at Kaggle. Founder of Animal Crossing Community. My Stack Overflow resume.

Updated on July 09, 2022

Comments

  • Jerad Rose
    Jerad Rose almost 2 years

    I have a fairly basic question: How can I check if a given value is contained in a list of enum values?

    For example, I have this enum:

    public enum UserStatus
    {
        Unverified,
        Active,
        Removed,
        Suspended,
        Banned
    }
    

    Now I want to check if status in (Unverified, Active)

    I know this works:

    bool ok = status == UserStatus.Unverified || status == UserStatus.Active;
    

    But there has to be a more elegant way to write this.

    The topic of this question is very similar, but that's dealing with flags enums, and this is not a flags enum.

    • Lav
      Lav about 13 years
      What is the datatype of status variable?
    • mistertodd
      mistertodd almost 4 years
      @Lav The variable status is of type UserStatus.
  • Jerad Rose
    Jerad Rose about 13 years
    Thanks, but this isn't really a matter of DRY, just more about concise code. I'm still not sure why C# doesn't have some sort of in operator that can be used for cases like this.
  • Jerad Rose
    Jerad Rose about 13 years
    I don't think this will work, as it is checking against every value in the enum, rather than a subset of values (if I'm reading correctly).
  • Aivan Monceller
    Aivan Monceller about 13 years
    @Jerad this works. give it a try. I have the same solution as what Lav posted
  • Jerad Rose
    Jerad Rose about 13 years
    @Avian - but where do you check for just Unverified or Active values (vs. the other three values)?
  • Aivan Monceller
    Aivan Monceller about 13 years
    @Jerad. my bad. answering questions from the office didn't give me much time reading your question.
  • Jerad Rose
    Jerad Rose about 13 years
    @Avian - I understand, no problem. Thanks for the attempt nonetheless.
  • Jerad Rose
    Jerad Rose about 13 years
    Yes, this is exactly what I was looking for. Again, not sure why this isn't built-in, but at least it's possible. :) Thanks!
  • Jan Thomä
    Jan Thomä about 9 years
    This is a very nice approach!
  • BlueStrat
    BlueStrat about 6 years
    Beautiful code... had no idea that the this type parameter of an extension method could be generic.
  • Paul McCarthy
    Paul McCarthy over 4 years
    Can't see how this checks against 2 out of 5 values
  • Rizan Zaky
    Rizan Zaky about 4 years
    .NET 4.0+ has a built-in function for this, Enum.HasFlag(Enum). In this example, you can use it like, bool ok = (UserStatus.Unverified | UserStatus.Active).HasFlag(status);. More here, stackoverflow.com/a/61389498/4294275
  • Eric G
    Eric G over 3 years
    Okay. I'm sorry. Really basic question. Where would this code belong?
  • RhinoDevel
    RhinoDevel about 2 years
    This only works, if the enum values are actually flags (1, 2, 4, 8, 16, 32, 64, ...).