7

Possible Duplicates:
What does placing a @ in front of a C# variable name do?
What’s the use/meaning of the @ character in variable names in C#?

As you can imagine, Googling or Binging for any phrase containing an '@' is difficult.

In creating a new web service, one of the members of the imported C# proxy class is prefixed with the @. For example:

plan.@event = new Insurance.Event();

I assume that it is Visual Studio's way resolving potential conflicts with reserved words because 'event' is a reserved word. Changing the property in the web service interface to something other than 'event' (i.e. 'healthevent') removes the @ from the property. Is this a correct assumption?

Community
  • 1
  • 1
Mike Chess
  • 2,758
  • 5
  • 29
  • 37
  • @Jorn Interesting that SO didn't display the item that you cited as a possible related item when I crafted this question. – Mike Chess Apr 16 '10 at 15:16
  • Well, the reason is probably the same as to why you did not find anything on Google/Bing. The title of this question and the one that I have linked really has no common keywords, except from `@` which is probably ignored by the search :) – Jørn Schou-Rode Apr 16 '10 at 15:52
  • Also, those comments are inserted automatically by SO's software when we select a close reason. I remembered a question like this recently; I didn't realize the one I recognized was itself a dupe of the one @Jorn found. – John Rudy Apr 16 '10 at 17:58

3 Answers3

8

Yes, names that conflict with C# keywords may be escaped with the @ character.

Bryan Watts
  • 44,911
  • 16
  • 83
  • 88
  • Actually all names may be preceded by the @ character, not only those that conflict with keywords. – Gabe Apr 16 '10 at 14:38
5

Yes that is correct. You can use keywords as identifiers as long as you include @ as a prefix.

See http://msdn.microsoft.com/en-us/library/x53a06bb.aspx for more info.

Iulian
  • 1,200
  • 5
  • 23
  • 47
2

You are correct. See C# Keywords, section 2.4.2 Identifiers of the language specification, or string (C# Reference).

From the keywords topic:

Keywords are predefined, reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example, @if is a valid identifier but if is not because if is a keyword.

Jeff Sternal
  • 47,787
  • 8
  • 93
  • 120