Enum 应该以 0 还是 1 开头? [英] Should an Enum start with a 0 or a 1?

查看:63
本文介绍了Enum 应该以 0 还是 1 开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我定义了以下枚举:

Imagine I have defined the following Enum:

public enum Status : byte
{
    Inactive = 1,
    Active = 2,
}

使用枚举的最佳实践是什么?它应该像上面的例子一样以 1 开头还是像这样以 0 开头(没有显式值):

What's the best practice to use enum? Should it start with 1 like the above example or start with 0 (without the explicit values) like this:

public enum Status : byte
{
    Inactive,
    Active
}

推荐答案

框架设计指南:

✔️ 务必在简单枚举上提供零值.

✔️ DO provide a value of zero on simple enums.

考虑将值称为无".如果这样的值不适合此特定枚举,则应为该枚举最常见的默认值分配基础值零.

Consider calling the value something like "None." If such a value is not appropriate for this particular enum, the most common default value for the enum should be assigned the underlying value of zero.

框架设计指南/设计标志枚举:

❌ 避免使用零的标志枚举值,除非该值表示所有标志都已清除"并按照下一个指南的规定进行了适当的命名.

❌ AVOID using flag enum values of zero unless the value represents "all flags are cleared" and is named appropriately, as prescribed by the next guideline.

✔️ 务必将标志枚举的零值命名为 None.对于标志枚举,该值必须始终表示清除所有标志."

✔️ DO name the zero value of flag enums None. For a flag enum, the value must always mean "all flags are cleared."

这篇关于Enum 应该以 0 还是 1 开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆