What does placing a @ in front of a C# variable name do?

Viewed 59217

I've been working with some C# legacy code and I've been seeing a lot of @ symbols in front of variable names. What does this signify or do?

Currently I'm seeing it a lot in front of variables with common names that aren't reserved. E.g.:

MyProcedure(@step.LoadInstanceId, @step.ResultCode, @step.StatusCode);

Given that step isn't a reserved word, is there any reason that they should be escaped?

6 Answers

It's just a way to allow declaring reserved keywords as vars.

void Foo(int @string)

It allows you to use a reserved word, like 'public' for example, as a variable name.

string @public = "foo";

I would not recommend this, as it can lead to unecessary confusion.

Putting @ in front of a string tells the compuler not to process escape sequences found within the string.

From the documentation:

The advantage of @-quoting is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:

@"c:\Docs\Source\a.txt"  // rather than "c:\\Docs\\Source\\a.txt"

To include a double quotation mark in an @-quoted string, double it:

@"""Ahoy!"" cried the captain." // "Ahoy!" cried the captain.

Another use of the @ symbol is to use referenced (/reference) identifiers that happen to be C# keywords. For more information, see 2.4.2 Identifiers.

This escapes reserved words in C#.

Related