C#枚举中的保留关键字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:19:38  来源:igfitidea点击:

Reserved Keyword in Enumeration in C#

提问by TobiasWittenburg

I would like to use asand isas members of an enumeration. I know that this is possible in VB.NET to write it like this:

我想使用asis作为枚举的成员。我知道在 VB.NET 中可以这样写:

Public Enum Test
    [as] = 1
    [is] = 2
End Enum

How do I write the equivalent statement in C#? The following code does not compile:

如何在 C# 中编写等效语句?以下代码无法编译:

public enum Test
{
    as = 1,
    is = 2
}

采纳答案by Brad Wilson

Prefixing reserved words in C# is done with @.

C# 中保留字的前缀是用@ 完成的。

public enum Test
{
    @as = 1,
    @is = 2
}

回答by Dale Ragan

You will need to prefix them with the @ symbol to use them. Here is the msdn pagethat explains it.

您需要在它们前面加上 @ 符号才能使用它们。这是解释它的msdn页面

回答by winwaed

It does seem like a bad idea though - like setting FIVE to equal 6.

不过,这似乎是个坏主意——比如将 FIVE 设置为 6。

Why not just use a predetermined prefix so that te names are unique and future maintainers of your code understand what you are doing?

为什么不使用预先确定的前缀,以便名称是唯一的,并且您的代码的未来维护者了解您在做什么?