枚举是否以0或1开头? [英] Should an Enum start with a 0 or a 1?

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

问题描述

想象一下,我已经定义了以下枚举:

Imagine I have defined the following Enum:

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

最佳做法是什么使用枚举?如果以上述示例开头,或者从 0开始 1 (不带显式值):

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 在非标记枚举中提供一个零值
    如果None不适用于枚举,则分配零对元素的价值 -
    应用作枚举的默认值。

  • Do provide a value of zero on your non-flags enum If None is not appropriate for the enum, then assign the zero-value to the element which should be used as the default value for the enum.

避免使用标志枚举对正负的成员或
为零。 ..枚举值为零会产生
操作等问题

Avoid using flag enum values normal members that are negative or zero. .. An enum value of zero creates problems with and operations, etc

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

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