C#禁用关键字功能

zma*_*ten 7 c# enums keyword

C#中有没有办法在代码中禁用关键字的功能?在我的情况下,我想将我的一个枚举项定义为浮动,这显然使Visual Studio有点混乱:)

public enum ValidationType
{
    email,
    number,
    float,
    range
}
Run Code Online (Sandbox Code Playgroud)

Dmi*_*nko 7

从技术上讲,你可以这样做:

public enum ValidationType
{
    email,
    number,
    @float, // note "@" before "float"
    range
}
Run Code Online (Sandbox Code Playgroud)

然而,即使可以使用关键词作为序数标识符,这也不是一个好习惯.在您的情况下,可能更好的解决方案是大写:

public enum ValidationType
{
    Email,
    Number,
    Float, 
    Range
}
Run Code Online (Sandbox Code Playgroud)


ram*_*sri 6

不是.关键字是预定义的保留标识符,对编译器有特殊含义.它们不能用作程序中的标识符,除非它们包含@作为前缀.

例如:

  • @if 是一个有效的标识符,但
  • if不是因为if是关键字.

https://msdn.microsoft.com/en-us/library/x53a06bb.aspx


Pio*_*app 5

您需要@在保留关键字之前使用如下:

public enum ValidationType
{
    email,
    number,
    @float,
    range
}
Run Code Online (Sandbox Code Playgroud)