Enum.HasFlag, why no Enum.SetFlag?

前端 未结 8 2094
别跟我提以往
别跟我提以往 2020-12-24 05:38

I have to build an extension method for each flag type I declare, like so:

public static EventMessageScope SetFlag(this EventMessageScope flags, 
    EventMe         


        
8条回答
  •  既然无缘
    2020-12-24 06:32

    The & operator will give you the same answer with a & b as it will with b & a, so

    (EventMessaageScope.Private).Get(EventMessageScope.Private | EventMessageScope.PublicOnly)

    is the same as writing

    (EventMessageScope.Private | EventMessageScope.PublicOnly).Get(EventMessaageScope.Private)

    If you just want to know if the value is the same as EventMessaageScope.Public, then just use equals:

    EventMessageScope.Private == EventMessageScope.Public

    Your method will always return false for (EventMessageScope.None).Get(EventMessaageScope.None) because None == 0 and it only returns true when the result of the AND operation is not zero. 0 & 0 == 0.

提交回复
热议问题