Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does adding the @ symbol make this work?

Tags:

c#

asp.net-mvc

I am working with asp.net mvc and creating a form. I want to add a class attribute to the form tag.

I found an example here of adding a enctype attribute and tried to swap out with class. I got a compile error when accessing the view.

I then found an example of someone adding a @ symbol to the beginning of the property name and that worked. Great that it works, but I am one that needs to know why and a quick Google search was not helpful. I understand that C# allows one to prepend the @ to a string to ignore escaping chars. Why does it work in this case? What does the @ tell the compiler?

Code that produces a compile error?

 <% Html.BeginForm("Results", "Search", 
    FormMethod.Get, new{class="search_form"}); %>

Code that does work:

 <% Html.BeginForm("Results", "Search", 
    FormMethod.Get, new{@class="search_form"}); %>
like image 205
GrillerGeek Avatar asked Mar 27 '09 18:03

GrillerGeek


People also ask

How do you add symbols at work?

Go to Insert > Symbol > More Symbols. Go to Special Characters. Double-click the character that you want to insert. Select Close.

What symbol is for Addition?

‌ A plus symbol or a horizontal line crossing a vertical line is also a symbol used as an indication to increase, add, move down, or zoom in on software programs and hardware devices.

How do I add a symbol to a number in Excel?

Go to Insert > Symbol. Pick a symbol, or choose More Symbols. Scroll up or down to find the symbol you want to insert. Different font sets often have different symbols in them and the most commonly used symbols are in the Segoe UI Symbol font set.

What is the and symbol in Excel?

Text concatenation operator in Excel is the ampersand symbol (&). You can use it to join two or more text strings in a single string. The same result can be achieved by using the CONCATENATE function, and the following tutorial explains all the details: How to combine text strings, cells and columns in Excel.


1 Answers

In C#, 'class' is a reserved keyword - adding an '@' symbol to the front of a reserved keyword allows you to use the keyword as an identifier.

Here's an example straight out of the C# spec:

class @class {
    public static void @static(bool @bool) {
        if (@bool) System.Console.WriteLine("true");
        else System.Console.WriteLine("false");
    }
}

Note: this is of course an example, and not recommended practice.

like image 160
Erik Forbes Avatar answered Oct 22 '22 00:10

Erik Forbes