C # keyword functionality

Is there a way in C # to disable the functionality of keywords in code? In my case, I want to define one of my enumeration elements as a float , which clearly makes Visual Studio a little confused :)

public enum ValidationType { email, number, float, range } 
+7
enums c # keyword
source share
4 answers

Technically, you can do it like this:

 public enum ValidationType { email, number, @float, // note "@" before "float" range } 

however, even if it is possible to use keywords as sequence identifiers, this is not a good practice. Probably the best solution in your case is to use:

 public enum ValidationType { Email, Number, Float, Range } 
+7
source share

Not. Keywords are predefined reserved identifiers that have special meanings for the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix.

For example:

  • @if is a valid identifier, but
  • if not because if is a keyword.

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

+6
source share

You need to use @ before the reserved keyword, as shown below:

 public enum ValidationType { email, number, @float, range } 
+5
source share

You do not need to disable anything, you can simply signal to the compiler using @ in front of the keyword name:

 public enum ValidationType { email, number, @float, range } 
+3
source share

All Articles