What does the @ in this code sample mean?
Asked Answered
P

3

6

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

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

Plugboard answered 12/11, 2008 at 16:39 Comment(0)
A
20

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.

Afflict answered 12/11, 2008 at 16:46 Comment(0)
K
8

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

Kilian answered 12/11, 2008 at 16:48 Comment(0)
D
7

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
Dynamotor answered 12/11, 2008 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.