c# - 是否可以在 C# 中使用作为关键字的值定义枚举?

标签 c# enums

我有一些正在读取的客户端数据,我已经为其中一个值定义了一个枚举,所以我可以使用 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/

相关文章:

ide - VB6 IDE 正在改变我的枚举名称的大小写

c# - 使用 C# 检查 Active Directory 中是否存在 UserID

c# - Asp.net c# 中的偏移 div 元素

c# - TxSelect 和 TransactionScope

c# - 发出代码效果不好

C Language : trouble using enumerated variable and type definited in main. c文件在另一个文件里面

c# - Aes-Gcm/Aes-Cbc - 数据库存储 - 列设计

asp.net-mvc - 使用 Automapper 将字符串映射到枚举

c - 用数组常量定义的枚举

java - libGDX 中的枚举出现 ExceptionInInitializerError