Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is @ in front of a variable / identifier in C#? [duplicate]

Tags:

syntax

c#

.net

I think my question is different from this: What's the @ in front of a string in C#?

I work in VB.net, so this may be some simple thing in C#, but I am not aware of this.

I got the following code where I have 10 XML inside a string variable. Please advice what @ symbol is needed in front of the claimsList string variable when calling LoadXml method in the code snippet below:

private void UploadNewClaims(PMAUser grumble, string companyAbbreviation, string claimsList)
{
    var claimDoc = new System.Xml.XmlDocument();
    claimDoc.LoadXml(@claimsList);
like image 405
Pawan Pillai Avatar asked Mar 06 '13 05:03

Pawan Pillai


People also ask

What's the in front of a string in C#?

It marks the string as a verbatim string literal. In C#, a verbatim string is created using a special symbol @. @ is known as a verbatim identifier. If a string contains @ as a prefix followed by double quotes, then compiler identifies that string as a verbatim string and compile that string.

What is prefix in C#?

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.

Can a variable name start with C#?

In C#, Variable names must start with a letter. Variable names are case-sensitive, num and Num are considered different names. Variable names cannot contain reserved keywords. False, In C#, a variable name can not start with a digit and white space character.


1 Answers

In this case it's completely unnecessary, but it allows you to use any keyword as an identifier in C#. It doesn't change the meaning of the identifier at all, or how it's used - it only tells the compiler that you don't want the following characters to be recognized as a keyword.

For example:

string @int = "hello";
var @void = @int;

Using it for an identifier of claimsList suggests that whoever wrote it doesn't understand it. The fact that the identifier is for a string variable is entirely irrelevant here.

Personally I've pretty much only ever used the feature for extension methods, where I have been known to call the first parameter @this:

public static void Foo(this Bar @this)
{
    return @this.Baz() * 2;
}
like image 188
Jon Skeet Avatar answered Oct 20 '22 23:10

Jon Skeet