开发者

C# @ in enum generated classes from XSD

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.


It escapes the default keyword from C#.

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


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'.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜