views:

446

answers:

3

Html.TextBox("ParentPassword", "", new { @class = "required" })

what the gosh darned heck is the @ for the @class.

+18  A: 

class is a reserved keyword, so you can't use this as a variable name.

The @ operator allows you to get around this rule. The reason why its being done here is that the anonymous object is used to populate attributes on a HTML element. A valid attribute name is "class", which lets you set the CSS class on the element.

Will
+8  A: 

class is a keyword. To use class as the name of a variable/property, in C#, you can prepend @ to it, as @class. In the IL, for all .net is concerned, the name of the variable/property is still class - @ is the way you have to do it in C#.

Justice
+6  A: 

Just to add my two cents to all the right answers here:

If you are new to C# but familiar to VB.NET, you probably know that there is a correspondent to @ in VB. The square brackets [ ] are used in VB.NET to surround a variable name that is named after a reserved word (or keyword). For example:

Dim [String] As String
splattne