3

Using xsd.exe I've got an enum which has an @ symbol in front of one of the elements. I can't work out why, and I can't work out what it's for or what it means. Searching Google for a symbol isn't terribly productive.

Original XSD fragment:

  <xs:simpleType name="stopLocation">
    <xs:restriction base="xs:string">
      <xs:enumeration value="default"/>
      <xs:enumeration value="near"/>
      <xs:enumeration value="far"/>
      <xs:enumeration value="nearExact"/>
      <xs:enumeration value="farExact"/>
    </xs:restriction>
  </xs:simpleType>

Generated class fragment:

public enum stopLocation {
    @default,
    near,
    far,
    nearExact,
    farExact,
}

(Yes, the final element has a comma which VS seems happy with)

Thanks.

GeoffM
  • 1,603
  • 5
  • 22
  • 34
  • probably because default is a C# keyword. – mdm20 Aug 29 '11 at 19:52
  • `default` is a reserved keyword; the `@` prefacing a reserved keyword lets you use it as a legal identifier (using it is a smell IMO (especially the disgusting `@this`)). The seemingly-extraneous `,` is legal as per the specification. It makes code maintenance and code generation a little bit easier. – jason Aug 29 '11 at 19:54
  • Ah, I was focusing on the @ being part of the enum rather than as a language feature - makes sense now! I'd never had the need to use it, hence not knowing it until now. I'll see if I can get the inherited XSD changed. Thanks everyone. – GeoffM Aug 29 '11 at 20:03

4 Answers4

5

It escapes the default keyword from C#.

See this question: What's the use/meaning of the @ character in variable names in C#?

Community
  • 1
  • 1
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1

This is happening because the enum value name (default) is a reserved word. In C# reserved words must be appended with an @ so the compiler knows how to interpret them.

You would see the same behavior with a name like 'event' or 'public'.

Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
1

default is a C# keyword.

The @ symbol is used as a way to escape language keywords so that they can be used as variable names and other identifiers.

Daniel Schaffer
  • 56,753
  • 31
  • 116
  • 165
0

http://dotnetdud.blogspot.com/2008/12/how-to-use-reserved-words-in-net-c.html

The "@" syntax allows you to use a reserved language word as a variable/member name. I would consider it poor practice in most cases.

Tim M.
  • 53,671
  • 14
  • 120
  • 163