tags:

views:

140

answers:

5

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.

+5  A: 

If you capitalize public to Public it won't be recognized as a keyword. Keywords are case sensitive.

As a general practice, however, it's a bad idea to use names that are keywords (even when they differ by case) as it can cause confusions, or even subtle defects if the keyword is accidentally used in place of the identifier.

It's also possible to use the @ in certain contexts (like variable or member declarations) to use reserved words as non-keywords. However, it's not a common practice and should only be a means of last resort, when you can't use a different name.

So in your case you could also use @public to use the reserved word as an enum identifier.

If you chose to use @, be aware that the symbol is only used in your source code to differentiate the identifier from the reserved word. To the outside world (and in methods like Enum.Parse()), the name of the enum value is simply public.

LBushkin
will the Parse method still work if the cases don't match?
Ferruccio
I realize this, but I don't have control over the fact that the client chose "public" as the value they want.
Eggplant Jeff
Parse has a flag that allows you to specify non-case-sensitive, though in my case it would be also inconvenient (though not a show-stopper) to have to specify it due to the helper classes I'm using.
Eggplant Jeff
When you use `@public` the `@` symbol is only used in your code to differentiate the identifer from the reserved word. To the outside world (including methods like `Enum.Parse()` you should use just `public`.
LBushkin
+7  A: 

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.

Oded
You mean prepend, not append.
Jon Skeet
@Jon Skeet - I do indeed. Thanks.
Oded
+8  A: 

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

Ferruccio
A: 

It's not really a great idea to do this though. Instead, add a bit more info to the enum:

PublicAccess etc

DanDan
I realize this, but I don't have control over the fact that the client chose "public" as the value they want.
Eggplant Jeff
A: 

In VB.Net use square braces [...] to delineate a keyword as an identifier. Example:

Public Sub Test(ByVal [public] As String)
  MessageBox.Show("Test string: " & [public])
End Sub
Nathan