attribute?.Description是什么意思?

stevenjin 2021-01-19 10:28:04
以下代码中的 attribute?.Description中那个?是啥意思?

   public static string GetDescription(Enum value, Boolean nameInstead = true) {
try {
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null) {
return null;
}

System.Reflection.FieldInfo field = type.GetField(name);
DescriptionAttribute attribute = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;

if (attribute == null && nameInstead == true) {
return name;
}
return attribute?.Description;
} catch {
return string.Empty;
}
}
...全文
184 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
键盘敲出字 2021-01-20
  • 打赏
  • 举报
回复
这是C#6.0的语法糖 Null 条件运算符 ?.

    return attribute?.Description;
    //等效于以下代码
    if (attribute != null){
        return attribute.Description;
    }
    else{
        return null;
    }

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧