我正在尝试为可空的枚举编写一个Extension方法.
与此示例一样:
// ItemType is an enum
ItemType? item;
...
item.GetDescription();
Run Code Online (Sandbox Code Playgroud)
所以我写了这个方法,由于某些我不理解的原因而无法编译:
public static string GetDescription(this Enum? theEnum)
{
if (theEnum == null)
return string.Empty;
return GetDescriptionAttribute(theEnum);
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误Enum?:
只有非可空值类型才能成为system.nullable的基础
为什么?Enum无法拥有这个价值null!
更新:
如果有很多枚举,ItemType只是其中之一的一个例子.