What are the pros and cons of using a flags enum?

[亡魂溺海] 提交于 2019-12-05 23:39:52

问题


I'm receiving several bit fields from hardware.

My code was originally:

public readonly byte LowByte;

public bool Timer { get { return (LowByte & 1) == 1; } }

Then I remembered the flags enum and am considering changing it to:

[Flags]
public enum LowByteReasonValues : byte
{
    Timer = 1,
    DistanceTravelledExceeded = 2,
    Polled = 4,
    GeofenceEvent = 8,
    PanicSwitchActivated = 16,
    ExternalInputEvent = 32,
    JourneyStart = 64,
    JourneyStop = 128
}

public readonly LowByteReasonValues LowByte;

public bool Timer { get { return (LowByte & LowByteReasonValues.Timer) == LowByteReasonValues.Timer; } }

and so on.

Which is best practice and what if any are the pros and cons of each approach?

EDIT: I'm interested to know if there are any practical differences between the two approaches, particularly in regards to performance. I'm not wishing to solicit opinion on coding styles (unless it comes from Microsoft guidelines) as that would see the question closed as unconstructive. Thanks.


回答1:


At the very least, your second example has better semantics and indicates the meaning of the bits within the code. There is some documentation within the code of what the bit is used for.

Otherwise, based on your first example, you will need to add comments since you are basically twiddling magic (bit) numbers, which makes the code much more difficult to read, especially by another person not familiar with it. Even if you yourself will be maintaining this code six months down the road, you may find it difficult to remember what bit 5 was used for.




回答2:


The later is best practice since it makes your code more readable




回答3:


If you are using .NET 4.0, you can now use the HasFlag method to check whether an enum contains a specific bit. This makes it even more readable than the previous method of checking.

[Flags]
public enum LowByteReasonValues : byte
{
    Timer = 1,
    DistanceTravelledExceeded = 2,
    Polled = 4,
    GeofenceEvent = 8,
    PanicSwitchActivated = 16,
    ExternalInputEvent = 32,
    JourneyStart = 64,
    JourneyStop = 128
}

public readonly LowByteReasonValues LowByte;

public bool Timer 
{
  get 
  { 
    return (LowByte.HasFlag(LowByte.Timer));
  } 
}

More information on MSDN.



来源:https://stackoverflow.com/questions/7906614/what-are-the-pros-and-cons-of-using-a-flags-enum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!