选择一个枚举类型的默认值,而无需更改值默认值、类型

2023-09-02 10:53:46 作者:康芒北鼻够

在C#中,是可以装饰一个枚举类型与属性或做别的事情来指定哪些默认值应该是,而无需更改的值?所需的数字可能在石头无论出于何种原因进行设置,并且它会是得心应手仍然有超过默认的控制。

 枚举方向
{
    无= -1,
    北= 0,
    东= 1,
    南= 2,
    西= 3
}

方向O; //是'北'默认。
 

解决方案

默认为枚举(其实,任何类型的值)为0 - 即使是不是该枚举的有效值。它不能被改变。

iOS iOS 13 适配要点总结

In C#, is it possible to decorate an Enum type with an attribute or do something else to specify what the default value should be, without having the change the values? The numbers required might be set in stone for whatever reason, and it'd be handy to still have control over the default.

enum Orientation
{
    None = -1,
    North = 0,
    East = 1,
    South = 2,
    West = 3
}

Orientation o; // Is 'North' by default.

解决方案

The default for an enum (in fact, any value type) is 0 -- even if that is not a valid value for that enum. It cannot be changed.

 
精彩推荐
图片推荐