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

后端 未结 8 1588
死守一世寂寞
死守一世寂寞 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:14

    For bitwise (Flags) enums, an "any of" test is != 0, so:

    const Tag flagsToLookFor = Tag.PrimaryNav | Tag.HomePage;
    if ((node.Tag & flagsToLookFor) != 0) {
        // has some cross-over with PrimaryNav or HomePage (and possibly others too) 
    }
    

提交回复
热议问题