gpt4 book ai didi

c# - 枚举应该以 0 还是 1 开头?

转载 作者:IT王子 更新时间:2023-10-29 03:33:08 25 4
gpt4 key购买 nike

假设我定义了以下枚举:

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

使用枚举的最佳做法是什么?它应该像上面的示例那样以 1 开头,还是像这样以 0 开头(没有显式值):

public enum Status : byte
{
Inactive,
Active
}

最佳答案

Framework Design Guidelines :

✔️ 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.

Framework Design Guidelines / Designing Flag Enums :

❌ 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.

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

关于c# - 枚举应该以 0 还是 1 开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7257409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com