Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define an enum in C# with values that are keywords?

Tags:

c#

enums

I have some client data that I am reading in, and I've defined an Enum for one of the values, so I can use Enum.Parse(type, somestring).

The problem is they just added a new value: "public". Is it possible to define an enum value that is also a reserved word?

I.E.:

public enum MyEnum {
    SomeVal,
    SomeOtherVal,
    public,
    YouGetTheIdea
}

If not I guess I'll be writing a parse method instead.

like image 914
Eggplant Jeff Avatar asked Aug 16 '10 16:08

Eggplant Jeff


2 Answers

You can prepend a @ to the variable name. This allows you to use keywords as variable names - so @public.

See here.

From the C# spec:

The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.

like image 121
Oded Avatar answered Oct 13 '22 20:10

Oded


yes, prefix the name with an @. i.e. @public

like image 39
Ferruccio Avatar answered Oct 13 '22 21:10

Ferruccio