为什么我不能像创建普通枚举那样在构造函数中创建可为空的枚举?
Why can't I create a nullable enum in a constructor the same way I can create a normal enum?
所以说这是我的 class:
class Devices
{
public enum deviceType { } // This works
public enum? deviceType { } // This doesn't work
}
那么为什么 public 不枚举? deviceType 不起作用?据我了解,? symbol 使 deviceType 枚举可以为 null;但是相反,第二行给我一个错误:
The name 'deviceType' does not exist in the current context.
这是一个非常基本的问题,但我有点困惑为什么我可以做一个而不能做另一个。
原来我对枚举的工作方式有误解(我认为它像字符串这样的变量而不是类型)。 @DavidG 评论说它需要格式化为:
public enum MyEnum {Value1, Value2}
private MyEnum? someEnumValue;
所以说这是我的 class:
class Devices
{
public enum deviceType { } // This works
public enum? deviceType { } // This doesn't work
}
那么为什么 public 不枚举? deviceType 不起作用?据我了解,? symbol 使 deviceType 枚举可以为 null;但是相反,第二行给我一个错误:
The name 'deviceType' does not exist in the current context.
这是一个非常基本的问题,但我有点困惑为什么我可以做一个而不能做另一个。
原来我对枚举的工作方式有误解(我认为它像字符串这样的变量而不是类型)。 @DavidG 评论说它需要格式化为:
public enum MyEnum {Value1, Value2}
private MyEnum? someEnumValue;