我有一些正在读取的客户端数据,我已经为其中一个值定义了一个枚举,所以我可以使用 Enum.Parse(type, somestring)。
问题是他们刚刚添加了一个新值:“public”。是否可以定义一个也是保留字的枚举值?
即:
public enum MyEnum {
SomeVal,
SomeOtherVal,
public,
YouGetTheIdea
}
如果没有,我想我会改写一个解析方法。
最佳答案
您可以在变量名前加上一个@
。这允许您使用关键字作为变量名 - 所以 @public
。
参见 here .
来自C# spec :
The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.
关于c# - 是否可以在 C# 中使用作为关键字的值定义枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3495104/