Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# disable keyword functionality

Tags:

c#

enums

keyword

Is there a way in C# to disable keyword's functionality in code? In my case I want to define one of my enum items as float which obviously makes Visual Studio a bit confused :)

public enum ValidationType
{
    email,
    number,
    float,
    range
}
like image 429
zmaten Avatar asked Sep 09 '15 08:09

zmaten


2 Answers

Technically, you can do it like that:

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

however, even if it's possible to use key words as ordinal identifiers it's not a good practice. Probably a better solution in your case is to capitalize:

public enum ValidationType
{
    Email,
    Number,
    Float, 
    Range
}
like image 126
Dmitry Bychenko Avatar answered Oct 27 '22 04:10

Dmitry Bychenko


No. Keywords are predefined, reserved identifiers that have special meanings to 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 is not because if is a keyword.

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

like image 20
ram hemasri Avatar answered Oct 27 '22 02:10

ram hemasri