Value is in enum list
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));
Use Enum.IsDefined
example:
public enum enStage {Work, Payment, Record, Return, Reject};
int StageValue = 4;
Enum.IsDefined(typeof(enStage), StageValue)
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.