选择一个枚举类型的默认值,而无需更改值 [英] Choosing the default value of an Enum type without having to change values

查看:181
本文介绍了选择一个枚举类型的默认值,而无需更改值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于选择一个枚举类型的默认值,而无需更改值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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