48

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.

2
  • What is the datatype of status variable? Commented Mar 16, 2011 at 3:08
  • @Lav The variable status is of type UserStatus. Commented Jun 5, 2020 at 13:42

8 Answers 8

86

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));
Sign up to request clarification or add additional context in comments.

4 Comments

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!
Beautiful code... had no idea that the this type parameter of an extension method could be generic.
.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
Okay. I'm sorry. Really basic question. Where would this code belong?
35

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.

Comments

9

Use Enum.IsDefined

example:

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

Enum.IsDefined(typeof(enStage), StageValue)

1 Comment

Can't see how this checks against 2 out of 5 values
3

While that wasn't possible when the question was first asked, since C# 9 you can express that in a much more concise way, thanks to pattern matching with the is operator:

bool ok = status is UserStatus.Unverified or UserStatus.Active;

Comments

2

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);

2 Comments

This only works, if the enum values are actually flags (1, 2, 4, 8, 16, 32, 64, ...).
I rolled back to the original answer as it 1) fundamentally changed its meaning, 2) was a copy of another user's answer that was posted 2 days earlier, 3) might still give value to some people that are looking for different approaches.
1

Why not create a method to encapsulate it?

public bool UnVerifiedOrActive(User user)
{
    return (user.UserStatus == UserStatus.Unverified || 
            user.UserStatus == UserStatus.Active);
}

1 Comment

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.
-1
UserStatus userStatus = null;
Enum.TryParse<UserStatus>(status.ToString(), out userStatus);

if (userStatus != null)
{
  //it is not in the list
}

6 Comments

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).
@Jerad this works. give it a try. I have the same solution as what Lav posted
@Avian - but where do you check for just Unverified or Active values (vs. the other three values)?
@Jerad. my bad. answering questions from the office didn't give me much time reading your question.
@Avian - I understand, no problem. Thanks for the attempt nonetheless.
|
-2

You can try following

UserStatus ustatus;
if(Enum.TryParse<UserStatus>(c.ToString(), out ustatus))
{
 ..Your logic
}

1 Comment

Tested, this doesn't work to determine whether it has a value or not. Enum.TryParse is still true for values not in enum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.