问题描述
我有这是一个枚举属性类
I have a class with a property which is an enum
枚举是
/// <summary>
/// All available delivery actions
/// </summary>
public enum EnumDeliveryAction
{
/// <summary>
/// Tasks with email delivery action will be emailed
/// </summary>
Email,
/// <summary>
/// Tasks with SharePoint delivery action
/// </summary>
SharePoint
}
当我创建这个类的一个实例,芳踪代码中,我指定的枚举字段的值,但它似乎默认为第一个项目在enumlist,而不是一个空值,这是枚举是如何工作的?这怎么可能,以确保枚举得到某种如果没有设置null值的,我不希望它默认为在枚举的第一个值。
When I create an instance of this class, NOWHERE in the code, do I specify the value of the enum field, but it seems to default to the first item in the enumlist, and not a null value, is this how enums work? How is it possible to ensure that the enum gets some kind of null value if it is not set, I don't want it defaulting to the first value in the enum.
推荐答案
的枚举
类型的默认值是 0
(这是在默认情况下,枚举中的第一个元素)。一类的字段将被初始化为默认值。
Default value for enum
types is 0
(which is by default, the first element in the enumeration). Fields of a class will be initialized to the default value.
如果您需要代表的未知的在枚举值,您可以添加元素未知
值为0。或者,你可以声明字段为可空< MyEnum>
( MyEnum?
)。
If you need to represent an unknown value in the enum, you can add an element Unknown
with value 0. Alternatively, you could declare the field as Nullable<MyEnum>
(MyEnum?
).
这篇关于枚举的初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!