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.
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]
You can prefix identifiers with @
so as to distinguish them from a reserved word etc:
var @class = 1;
(@class, for example, os mainly used in razor views for specifying css class. Otherwise, avoid using such names :)
Also useful in LINQ like from @group in...
Don't downvote this because the question said it was in a regular .cs
class. At the time I wrote this, there was nothing in the question to indicate this wasn't included in a razor view. And given no context, it would make much more sense for that to be the case.
I'm keeping this answer here in case other users get here looking for what @
might do.
Its a way to access server-side variables and methods from a razor view (.cshtml
).
It's very commonly used to:
Read session variables:
@Session["something"]
Access Html helper methods
@Html.RenderPartial("someview","somecontroller");
Do exactly what you have in the question(though personally I think it's bad practice)
@ConfigurationManager.AppSettings["whatever"];
Start code blocks and use things like foreach
@{
foreach(var item in Model)
{
<span class="modelProperty">@Model.SomeTextProperty</span>
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With