I discovered that you can start your variable name with a '@' character in C#. In my C# project I was using a web service (I added a web reference to my project) that was written in Java. One of the interface objects defined in the WSDL had a member variable with the name "params". Obviously this is a reserved word in C# so you can't have a class with a member variable with the name "params". The proxy object that was generated contained a property that looked like this:
public ArrayList @params {
get { return this.paramsField; }
set { this.paramsField = value; }
}
I searched through the VS 2008 c# documentation but couldn't find anything about it. Also searching Google didn't give me any useful answers. So what is the exact meaning or use of the '@' character in a variable/property name?
Unlike Perl's sigils, an
@
prefix before a variable name in C# has no meaning. Ifx
is a variable,@x
is another name for the same variable.But the
@
prefix does have a use, as you've discovered - you can use it to clarify variables names that C# would otherwise reject as illegal.Another use-case is in extension methods. The first, special parameter can be distinguished to denote its real meaning with
@this
name. An example:It simply allows you to use reserved words as variable names. I wanted a var called
event
the other day. I was going to go with_event
instead, but my colleague reminded me that I could just call it@event
instead.