Tag: 枚举可以为

可以为空的枚举的扩展方法

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