7

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);
Community
  • 1
  • 1
Pawan Pillai
  • 1,955
  • 5
  • 37
  • 64

1 Answers1

21

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;
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    I imagine that the @ is there because at one point it was a path string literal such as @"C:\path". I assume that when it was changed to be a variable the person making the change didn't understand the @"" syntax and left the @ in place. – Grokys Mar 07 '13 at 22:23
  • And I've googled that because I've seen `@this` as parameter for an extension method hah. Hell thats useful. A point that is shown but not written: `You have to use @variable also when calling, not only for the declaration`. – C4d Aug 18 '16 at 14:25