Use of "default" as an enum in C#

I have a quick question regarding the use of "default" as an enum value. I'm reading the contents of an existing XML file and parsing the element values to enums. Here is and example of what is in the XML file:

<userpref_load_policy>default</userpref_load_policy>

Unfortunately I cannot do the following as 'default' is a keyword! 'at_start' and 'in_background' are the other permitted values for this element.

public enum LoadPolicy { default, at_start, in_background }

Is there an "easy" way to use default as an enum value or trick the compliler? I have a nasty feeling I will need to resort to a custom extension method. I say this as I'm calling the .ToString() method when writing the XML file back out (and there are lots of enums..)

Thanks in advance for any pointers, Iain

Jon Skeet
people
quotationmark

Yes, you can use it as an identifier using @:

public enum LoadPolicy { @default, at_start, in_background }

From the C# 5 spec, section 2.4.2:

The rules for identifiers given in this section correspond exactly to those recommended by the Unicode Standard Annex 31, except that underscore is allowed as an initial character (as is traditional in the C programming language), Unicode escape sequences are permitted in identifiers, and the "@" character is allowed as a prefix to enable keywords to be used as identifiers.

I would personally prefer to use more idiomatic names within the enum itself, but have a mapping between the enum values and their XML representations. But this will work if you really have to use the enum value names...

people

See more on this question at Stackoverflow