Alex Rivera | Logout

'@' symbol prefix on c# method call?

Asked 2011-08-30T15:20:17.747
9

Possible Duplicate:
What's the use/meaning of the @ character in variable names in C#?

I was doing some .net development and I noticed I could add an @ symbol before any method call, i.e.:

var message = dog.@SayHello();

Just wondering, why is the reason this can be done ??

Edit
Report

2 Answers

6

An @ symbol can be pre-fixed to any identifier in case you're interoperating with code written in another language that uses C# keywords as identifiers:

object @class; //allowed
object class;  //error
answered 2011-08-30T15:22:03.107
3

@ lets you use an identifier that would otherwise be interpreted as a keyword.

For example, if you want to have a variable named class, you can write this:

int @class;

Of course, because you can do it doesn't mean it's a good idea to do it.

Some situations where it's useful is code generators that create C# code from a template or external source. e.g. xsd.exe is an SDK command-line tool creates C# classes that back up an XML schema. If your XML schema contains an element or attribute name that is reserved in C# (such as class), xsd.exe would preserve the name in the matching C# class but it would prefix it with a @.

answered 2011-08-30T15:24:08.990

Your Answer