Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reserved Keyword in Enumeration in C#

Tags:

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:

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

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

public enum Test
{
    as = 1,
    is = 2
}
like image 365
TobiasWittenburg Avatar asked Aug 24 '08 19:08

TobiasWittenburg


People also ask

What is meant by reserved keyword?

Reserved keywords in C++? A reserved word is a word that cannot be used as an identifier, such as the name of a variable, function, or label – it is "reserved from use". This is a syntactic definition, and a reserved word may have no meaning. There are a total of 95 reserved words in C++.

What is enum keyword in C?

In C programming, an enumeration type (also called enum) is a data type that consists of integral constants. To define enums, the enum keyword is used. enum flag {const1, const2, ..., constN}; By default, const1 is 0, const2 is 1 and so on.

What is a reserved word examples?

Often found in programming languages and macros, reserved words are terms or phrases appropriated for special use that may not be utilized in the creation of variable names. For example, "print" is a reserved word because it is a function in many languages to show text on the screen.


2 Answers

Prefixing reserved words in C# is done with @.

public enum Test
{
    @as = 1,
    @is = 2
}
like image 124
Brad Wilson Avatar answered Oct 13 '22 12:10

Brad Wilson


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

like image 23
Dale Ragan Avatar answered Oct 13 '22 12:10

Dale Ragan