336

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?

Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
Remko Jansen
  • 4,649
  • 4
  • 30
  • 39

9 Answers9

350

Straight from the C# Language Specification, Identifiers (C#) :

The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier.

Sampath Dilhan
  • 749
  • 6
  • 17
Atif Aziz
  • 36,108
  • 16
  • 64
  • 74
  • 2
    what is the targeted minimal version of .NET supporting `@`? – serhio Jul 05 '10 at 21:08
  • 20
    .NET itself doesn't define the `@` symbol like this, the C# language specification does. It has supported this since its first version, C# 1.0 (released with .NET 1.0). http://csharpindepth.com/articles/chapter1/Specifications.aspx – Tim S. Jun 04 '13 at 14:00
  • 11
    And for those wondering, in VB.NET you can use `[ ]` to specify a verbatim identifier, e.g. `Dim [String] As String`. – MicroVirus Apr 28 '14 at 11:24
83

It just lets you use a reserved word as a variable name. Not recommended IMHO (except in cases like you have).

rslite
  • 81,705
  • 4
  • 44
  • 47
  • 9
    @rslite: +1 Not recommended :) – user7116 Sep 18 '08 at 12:14
  • 5
    > *never* +1 Not recommended, but never say never. You may for example need to implement a legacy COM interface that uses a C# keyword as an identifier. Or Microsoft may introduce new keywords in new versions of the Framework - e.g. yield in a .NET 1.1 Bond trading app :) – Joe Sep 18 '08 at 18:15
  • 2
    @Joe: The new `yield` keyword is not a reserved word, and is only usable on contexts where *no identifier could legally appear*. One goal when designing new features for C# is to construct them in such a way that any program which would be legal before a new feature was added will be legal afterward *and have the same meaning*. – supercat Sep 06 '13 at 22:50
  • 7
    `@Html.TextboxFor( , , , new { @class="my-css-class" } )` is a good example where you can't really get around it without having to manually write the HTML, or have javascript change the attributes at a later stage. – Flater May 19 '14 at 11:57
  • There is a new trend that is seen in Open Source software where all variables are prefixed with the "@" symbol in C# code. I believe this may be because of the familiarity of this requirement in PHP software for variables. And a lot of Open Source web systems are now coded in C# (where in the past it would have been PHP) – Wasted_Coder Mar 05 '16 at 19:36
  • Or any keyword, whether reserved or not. `from` is an unreserved keyword by the looks of it - I just saw some code in which a method parameter is declared as `string from`, but within the method body it is referenced as `@from`, presumably to prevent it from being interpreted as the start of a LINQ expression. – Stewart May 30 '16 at 10:26
39

In C# the at (@) character is used to denote literals that explicitly do not adhere to the relevant rules in the language spec.

Specifically, it can be used for variable names that clash with reserved keywords (e.g. you can't use params but you can use @params instead, same with out/ref/any other keyword in the language specification). Additionally it can be used for unescaped string literals; this is particularly relevant with path constants, e.g. instead of path = "c:\\temp\\somefile.txt" you can write path = @"c:\temp\somefile.txt". It's also really useful for regular expressions.

Faust
  • 15,130
  • 9
  • 54
  • 111
Tomer Gabel
  • 4,104
  • 1
  • 33
  • 37
  • Interesting point (and useful mnemonic) that the convention is the same between "verbatim-" or "[here-strings](https://devblogs.microsoft.com/powershell/variable-expansion-in-strings-and-here-strings/)" ***and*** parameter naming. – ruffin Mar 21 '19 at 15:39
15

Unlike Perl's sigils, an @ prefix before a variable name in C# has no meaning. If x is a variable, @x is another name for the same variable.

> string x = "abc";
> Object.ReferenceEquals(x, @x).Dump();
True

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.

> string string;
Identifier expected; 'string' is a keyword

> string @string;
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
14

The @ symbol allows you to use reserved keywords for variable name. like @int, @string, @double etc.

For example:

string @public = "Reserved Keyword used for me and its fine";

The above code works fine, but below will not work:

string public = "This will not compile";
Umar Abbas
  • 4,399
  • 1
  • 18
  • 23
5

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.

Mark Embling
  • 12,605
  • 8
  • 39
  • 53
3

Another use case are extension methods. The first, special parameter can be distinguished to denote its real meaning with @this name. An example:

public static TValue GetValueOrDefault<TKey, TValue>(
    this IDictionary<TKey, TValue> @this,
    TKey key,
    TValue defaultValue)
    {
        if (!@this.ContainsKey(key))
        {
            return defaultValue;
        }

        return @this[key];
    }
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
1

You can use it to use the reserved keywords as variable name like

 int @int = 3; 

the compiler will ignores the @ and compile the variable as int

it is not a common practice to use thought

Mina Gabriel
  • 23,150
  • 26
  • 96
  • 124
1

If we use a keyword as the name for an identifier, we get a compiler error “identifier expected, ‘Identifier Name’ is a keyword” To overcome this error, prefix the identifier with “@”. Such identifiers are verbatim identifiers. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix