选择 Enum 类型的默认值而不必更改值 [英] Choosing the default value of an Enum type without having to change values

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

问题描述

在 C# 中,是否可以在不更改值的情况下使用属性装饰 Enum 类型或执行其他操作来指定默认值应该是什么?无论出于何种原因,所需的数字都可能是一成不变的,而且仍然可以控制默认值会很方便.

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.

推荐答案

enum(实际上,任何值类型)的默认值是 0——即使这不是一个有效的值那个 enum.无法更改.

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.

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

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