假设你有
enum MyEnum {A = 0, B = 1, C = 2, D = 4, E = 8, F = 16};
在某些时候,你有一个函数会检查MyEnum的一个实例,如果它是C,D或F则返回true
bool IsCDF(MyEnum enumValue)
{
return //something slick
}
我记得有一些非常灵巧的方法可以进行位移和预先形成这种操作,这种操作比一堆三元if语句读得更好,但对于我的生活,我不记得它是什么。
有人知道吗?
答案 0 :(得分:19)
bool IsCDF(MyEnum enumValue)
{
return new[]{MyEnum.C, MyEnum.D, MyEnum.F}.Contains(enumValue);
}
答案 1 :(得分:18)
如果将其设为[Flags]
枚举,则可以为每个枚举值分配不同的位值(1,2,4,8,1 ...)。然后,您可以使用按位运算来确定值是否是一组可能值之一。
所以,看看它是C,D还是F:
bool IsCDF(MyEnum enumValue)
{
return ((enumValue & (MyEnum.C | MyEnum.D | MyEnum.F)) != 0);
}
或使用HasFlag()
(效率较低但更易读):
bool IsCDF(MyEnum enumValue)
{
return enumValue.HasFlag(MyEnum.C | MyEnum.D | MyEnum.F);
}
请注意,这不适用于值0(在您的示例中为'A'),并且必须注意所有枚举值都会解析为唯一的位值(即非零幂的2)。
这种方法的优点是:
得心应手提示:
在定义[Flags]枚举时,使用左移(<<
)使位值更清晰(并且更难以出错),尤其是对于高阶位:
[Flags]
enum MyEnum
{
A = 1 << 0, // Equivalent to 1
B = 1 << 1, // Equivalent to 2
C = 1 << 2, // Equivalent to 4
D = 1 << 3, // Equivalent to 8
…
Big = 1 << 26, // Equivalent to 67108864
}
答案 2 :(得分:5)
我可能会使用Unconstrained Melody来保持整洁:
if (value.HasAny(MyEnum.C | MyEnum.D | MyEnum.E))
{
...
}
我可能会将“C,D或E”位提取到命名常量中 - 可能在枚举本身中,如果它有意义:
答案 3 :(得分:2)
你可能在考虑FlagsAttribute。请查看here和here以获取一些示例。
您可以使用Enum.HasFlag Method
答案 4 :(得分:1)
也许这个扩展类对你有用:
public static class Flags
{
/// <summary>
/// Checks if the type has any flag of value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool HasAny<T>(this System.Enum type, T value)
{
try
{
return (((int) (object) type & (int) (object) value) != 0);
}
catch
{
return false;
}
}
/// <summary>
/// Checks if the value contains the provided type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool Has<T>(this System.Enum type, T value)
{
try
{
return (((int)(object)type & (int)(object)value) == (int)(object)value);
}
catch
{
return false;
}
}
/// <summary>
/// Checks if the value is only the provided type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool Is<T>(this System.Enum type, T value)
{
try
{
return (int)(object)type == (int)(object)value;
}
catch
{
return false;
}
}
/// <summary>
/// Appends a value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <param name="value"></param>
/// <returns></returns>
public static T Add<T>(this System.Enum type, T value)
{
try
{
return (T)(object)(((int)(object)type | (int)(object)value));
}
catch (Exception ex)
{
throw new ArgumentException(
string.Format(
"Could not append value from enumerated type '{0}'.",
typeof(T).Name
), ex);
}
}
/// <summary>
/// Appends a value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <param name="value"></param>
/// <returns></returns>
public static void AddTo<T>(this System.Enum type, ref T value)
{
try
{
value = (T)(object)(((int)(object)type | (int)(object)value));
}
catch (Exception ex)
{
throw new ArgumentException(
string.Format(
"Could not append value from enumerated type '{0}'.",
typeof(T).Name
), ex);
}
}
/// <summary>
/// Removes the value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <param name="value"></param>
/// <returns></returns>
public static T Remove<T>(this System.Enum type, T value)
{
try
{
return (T)(object)(((int)(object)type & ~(int)(object)value));
}
catch (Exception ex)
{
throw new ArgumentException(
string.Format(
"Could not remove value from enumerated type '{0}'.",
typeof(T).Name
), ex);
}
}
/// <summary>
/// Removes the value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <param name="value"></param>
/// <returns></returns>
public static void RemoveFrom<T>(this System.Enum type, ref T value)
{
try
{
value = (T)(object)(((int)(object)type & ~(int)(object)value));
}
catch (Exception ex)
{
throw new ArgumentException(
string.Format(
"Could not remove value from enumerated type '{0}'.",
typeof(T).Name
), ex);
}
}
}
答案 5 :(得分:0)
return (enumValue & MyEnum.C == MyEnum.C)
|| (enumValue & MyEnum.D == MyEnum.D)
|| (enumValue & MyEnum.F == MyEnum.F);