传统方法是在上使用Flags
属性enum
:
[Flags]
public enum Names
{
None = 0,
Susan = 1,
Bob = 2,
Karen = 4
}
然后,您将按照以下方式检查特定名称:
Names names = Names.Susan | Names.Bob;
// evaluates to true
bool susanIsIncluded = (names & Names.Susan) != Names.None;
// evaluates to false
bool karenIsIncluded = (names & Names.Karen) != Names.None;
逻辑按位组合可能很难记住,因此我可以通过一FlagsHelper
堂课* 使自己的生活更轻松:
// The casts to object in the below code are an unfortunate necessity due to
// C#'s restriction against a where T : Enum constraint. (There are ways around
// this, but they're outside the scope of this simple illustration.)
public static class FlagsHelper
{
public static bool IsSet<T>(T flags, T flag) where T : struct
{
int flagsValue = (int)(object)flags;
int flagValue = (int)(object)flag;
return (flagsValue & flagValue) != 0;
}
public static void Set<T>(ref T flags, T flag) where T : struct
{
int flagsValue = (int)(object)flags;
int flagValue = (int)(object)flag;
flags = (T)(object)(flagsValue | flagValue);
}
public static void Unset<T>(ref T flags, T flag) where T : struct
{
int flagsValue = (int)(object)flags;
int flagValue = (int)(object)flag;
flags = (T)(object)(flagsValue & (~flagValue));
}
}
这将允许我将以上代码重写为:
Names names = Names.Susan | Names.Bob;
bool susanIsIncluded = FlagsHelper.IsSet(names, Names.Susan);
bool karenIsIncluded = FlagsHelper.IsSet(names, Names.Karen);
注意,我还可以Karen
通过执行以下操作添加到集合中:
FlagsHelper.Set(ref names, Names.Karen);
我可以Susan
用类似的方式删除:
FlagsHelper.Unset(ref names, Names.Susan);
*正如Porges所指出的,IsSet
.NET 4.0中已经存在上述方法的等效项:Enum.HasFlag
。不过,Set
和Unset
方法似乎没有等效项。所以我还是要说这堂课有一些优点。
注意:使用枚举只是解决此问题的常规方法。您可以完全翻译上面的所有代码以改为使用int,它也将正常工作。