使用标志枚举有哪些优点和缺点?

发布于 2024-12-12 02:43:40 字数 752 浏览 0 评论 0原文

我从硬件接收到几个位字段。

我的代码最初是:

public readonly byte LowByte;

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

然后我记得标志枚举并正在考虑将其更改为:

[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; } }

等等。

哪种方法是最佳实践?每种方法的优缺点是什么?

编辑:我很想知道这两种方法之间是否存在任何实际差异,特别是在性能方面。我不想征求有关编码风格的意见(除非它来自 Microsoft 指南),因为这会使问题被认为没有建设性。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

英雄似剑 2024-12-19 02:43:40

后者是最佳实践,因为它使您的代码更具可读性

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

烈酒灼喉 2024-12-19 02:43:40

如果您使用的是 .NET 4.0,现在可以使用 HasFlag 方法来检查枚举是否包含特定位。这使得它比以前的检查方法更具可读性。

[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));
  } 
}

有关 MSDN 的更多信息。

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.

把人绕傻吧 2024-12-19 02:43:40

至少,您的第二个示例具有更好的语义并指示代码中位的含义。代码中有一些文档说明了该位的用途。

否则,根据您的第一个示例,您将需要添加注释,因为您基本上是在摆弄魔术(位)数字,这使得代码更难以阅读,尤其是对于另一个不熟悉它的人来说。即使您自己将在六个月后维护此代码,您也可能会发现很难记住第 5 位的用途。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文