相关疑难解决方法(0)

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

我正在尝试为可空的枚举编写一个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只是其中之一的一个例子.

.net c# compiler-construction enums nullable

10
推荐指数
1
解决办法
2392
查看次数

标签 统计

.net ×1

c# ×1

compiler-construction ×1

enums ×1

nullable ×1