How do I determine if an Enum value has one or more of the values it's being compared with?

后端 未结 8 1584
死守一世寂寞
死守一世寂寞 2021-01-04 07:53

I\'ve got an Enum marked with the [Flags] attribute as follows:

[Flags]
public enum Tag : int
{
    None = 0,
    PrimaryNav = 1,
    HideChildPages = 2,
            


        
8条回答
  •  春和景丽
    2021-01-04 08:10

    You can use the HasFlag Method to avoid the need for the boolean logic,

    Tag Val = (Tag)9;
    
    if (Val.HasFlag(Tag.PrimaryNav))
    {
        Console.WriteLine("Primary Nav");
    }
    
    if(Val.HasFlag(Tag.HomePage))
    {
        Console.WriteLine("Home Page");
    }
    

提交回复
热议问题