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
Reserved Keyword in Enumeration in C#
提问by TobiasWittenburg
I would like to use as
and is
as members of an enumeration. I know that this is possible in VB.NET to write it like this:
我想使用as
和is
作为枚举的成员。我知道在 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
回答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?
为什么不使用预先确定的前缀,以便名称是唯一的,并且您的代码的未来维护者了解您在做什么?