Cade Roux Cade Roux - 3 months ago 19
C# Question

C# @ operator (not for string literals)

What does the

@
in this mean (I know it's using an obsolete .NET Framework 1.1 ConfigurationSettings.AppSettings)?

@ConfigurationSettings.AppSettings["some_setting"];


This is NOT a string literal: using the literal '@' with a string variable

The actual code:

_scale_id_regex = @ConfigurationSettings.AppSettings["ScaleIdRegEx"];


In a regular .cs file which is part of a Windows Service and
_scale_id_regex
is just a private string in the class, so ASP.NET and Razor are not involved.

Answer

It's called a verbatim identifier. It allows you to name variables after reserved words. e.g.

string @string = string.Empty;
object @object = new object();
int @int = 1;
...

The code you have is valid, but I don't believe @ services any real purpose there. Since this got upvoted faster than I could refresh my page, here's what the ECMA C# Language Specification, section 9.4.2 says.

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.

The code you posted is valid because this is allowed by the language specification, albeit discouraged.

[Note: Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style. end note]

Comments