C# 用空值初始化枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13605303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initialize enum with a null value
提问by Hélder Gon?alves
How do i create an enum with a null value
我如何创建一个带有空值的枚举
ex:
前任:
public enum MyEnum
{
[StringValue("X")]
MyX,
[StringValue("Y")]
MyY,
None
}
where None value is null or String.Empty
其中 None 值为 null 或 String.Empty
采纳答案by Rahul Tripathi
You can try something like this:-
你可以尝试这样的事情:-
public MyEnum? value= null;
or you can try this:-
或者你可以试试这个:-
public MyEnum value= MyEnum.None;
回答by Oded
All enum types are value types and the different members are also derived from member types (the allowed types are byte, sbyte, short, ushort, int, uint, long, or ulong- as documented).
所有枚举类型都是值类型,不同的成员也派生自成员类型(允许的类型是byte, sbyte, short, ushort, int, uint, long, 或ulong- 如文档所述)。
This means that members of an enumeration cannot be null.
这意味着枚举的成员不能是null.
One way to deal with a default enumvalue is to put the Nonevalue as the first value and setting it to a negative value (for signed enums) or 0.
处理默认enum值的一种方法是将该None值作为第一个值并将其设置为负值(对于有符号枚举)或0.
public enum MyEnum
{
[StringValue(null)]
None = -1,
[StringValue("X")]
MyX,
[StringValue("Y")]
MyY
}
You can have a nullreference to an enumeration if you declare a nullable instance - myenum?.
null如果您声明了一个可为空的实例 - ,则您可以拥有对枚举的引用myenum?。
回答by shaouari
use the "?" oeprator for a nullable type !!
使用 ”?” 可空类型的运算符!!
public MyEnum? myEnum= null;
回答by Marc Gravell
There is no such thing as an "enum with a null value". An enum in C# is just a named integer. In your case, Noneis just an alias for 2(for completeness, MyXis 0and MyYis 1). By default, the underlying data-type of an enum is int, but it can also be other integer-based primitive types of different sizes/sign.
没有“具有空值的枚举”这样的东西。C# 中的枚举只是一个命名整数。在您的情况下,None只是2(为了完整性,MyXis0和MyYis 1)的别名。默认情况下,枚举的基础数据类型是int,但它也可以是不同大小/符号的其他基于整数的原始类型。
If you want a "null" enum, you would have to use MyEnum?, aka Nullable<MyEnum>.
如果您想要一个“空”枚举,则必须使用MyEnum?, aka Nullable<MyEnum>.
回答by Blachshma
As all the other answers said, you can'thave an enum value be null.
What you cando is add a Descriptionattribute (like your StringValue) which has a null value.
由于所有其他的答案说,你不能拥有为空的枚举值。您可以做的是添加一个具有空值的Description属性(如您的StringValue)。
For instance:
例如:
public enum MyEnum
{
[Description(null)]
None = 0,
[Description("X")]
MyX,
[Description("Y")]
MyY
}
You can get the description of the Enum with the following function:
您可以使用以下函数获取 Enum 的描述:
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null && attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
Usage:
用法:
MyEnum e = MyEnum.None;
string s2 = GetEnumDescription((MyEnum)e); // This returns null

