Enum.HasFlag, why no Enum.SetFlag?

前端 未结 8 2093
别跟我提以往
别跟我提以往 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

    To answer part of your your question: the Get function works properly according to binary logic - it checks for any match. If you want to match the whole set of flags, consider this instead:

    return ((flags & flag) != flag);
    

    Regarding "why isn't there SetFlag"... probably because it's not really needed. Flags are integers. There is already a convention for dealing with those and it applies to flags as well. If you don't want to write it with | and & - that's what the custom static addons are for - you can just use your own functions as you demonstrated yourself :)

提交回复
热议问题