且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

利弊明确设置枚举字段的值

更新时间:2022-05-28 21:50:45

Enum1和Enum2编译为相同的枚举.因此,从编程的角度来看,没有区别.从维护的角度来看,可能会出现问题,也可能不会出现问题.

Enum1 and Enum2 compile to the same enumeration. So from a programming standpoint, there is no difference. From a maintenance standpoint, there may or may not be issues.

一方面,对于 [Flags] 枚举,绝对需要声明显式值;并且它们必须是2的幂的值.

On the one hand, declaring explicit values is absolutely required for a [Flags] enum; and they need to be values at powers of 2.

另一方面,显式值可以简单地通过创建将来无法更改或不应该更改的值来降低可维护性.

On the other hand, explicit values can reduce maintainability simply by creating values that cannot or should not be changed in the future.

通常,我宁愿不声明显式值,除非值本身由于某些原因而有意义-例如,如果您的C#枚举是包装C枚举.

In general, I prefer to not declare explicit values unless the values themselves are significant for some reason -- if your C# enumeration is, for example, wrapping a C enumeration.