c# - 使用枚举处理强类型

标签 c# enums

如果我定义了一个从 short 继承的 enums,我对 enums 有疑问:

public enum ProyectoEstatus : short
{
    EstatusPorDefecto = 26,
    AprobadoUnidadNegocio = 6,
    CanceladoAreaComercial = 18
}

为什么我不能这样做??

Nullable<short> aux = ProyectoEstatus.CanceladoAreaComercial as ProyectoEstatus;

如果我的名为 aux 的变量类型是 Nullable

最佳答案

首先,enum类型本身不可为空,因此 as运算符(operator)不会处理它。

其次,enum type 实际上不是 short .这是一个short -backed enum 类型,但它需要显式转换为 short 才能接受来自 short隐式转换至 Nullable<short> :

    Nullable<short> aux = (short)ProyectoEstatus.CanceladoAreaComercial 

关于c# - 使用枚举处理强类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9178832/

相关文章:

c# - jquery ajax 'post' 调用

c# - 设置控件的属性时,ASP.NET 引号字符编码会导致问题

c# - 弹出窗口为 View 中的每个窗口显示相同的结果

c# - System.Enum 作为带有约束的泛型类型参数

java - 使用枚举中实现 "Listeners"的类

java - jackson 数据绑定(bind)枚举不区分大小写

c++ - 来自 int C++ 类的无效转换

c# - 使用 C# 控制台应用程序创建 HTTP 发布请求并接收响应

c# - Pomelo 使用 MySQL 和 .NET Core 生成不正确的 SQL

java - 在 querydsl 上使用枚举时出现错误类型