Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# @ operator (not for string literals)

Tags:

c#

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.

like image 253
Cade Roux Avatar asked Aug 07 '12 18:08

Cade Roux


3 Answers

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]

like image 163
David Anderson Avatar answered Sep 29 '22 11:09

David Anderson


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...

like image 37
manojlds Avatar answered Sep 29 '22 12:09

manojlds


Precusor:

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.

Answer:

Its a way to access server-side variables and methods from a razor view (.cshtml). It's very commonly used to:

  1. Read session variables:

    @Session["something"]

  2. Access Html helper methods

    @Html.RenderPartial("someview","somecontroller");

  3. Do exactly what you have in the question(though personally I think it's bad practice)

    @ConfigurationManager.AppSettings["whatever"];

  4. Start code blocks and use things like foreach

    @{
        foreach(var item in Model)
        {
            <span class="modelProperty">@Model.SomeTextProperty</span>
        }
    }
    
like image 40
Phillip Schmidt Avatar answered Sep 29 '22 11:09

Phillip Schmidt