在C#中,是可以装饰一个枚举类型与属性或做别的事情来指定哪些默认值应该是,而无需更改的值?所需的数字可能在石头无论出于何种原因进行设置,并且它会是得心应手仍然有超过默认的控制。
枚举方向
{
无= -1,
北= 0,
东= 1,
南= 2,
西= 3
}
方向O; //是'北'默认。
解决方案
默认为枚举
(其实,任何类型的值)为0 - 即使是不是该枚举
的有效值。它不能被改变。
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.
上一篇:什么是你最喜欢的LINQ的方法或“绝招”最喜欢、绝招、方法、LINQ
下一篇:prevent的Visual Studio自动创建"混合平台和QUOT;解决方案配置解决方案、平台、Visual、prevent