DevelopingChris
DevelopingChris

Reputation: 40788

What does the @ in this code sample mean?

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

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

Upvotes: 6

Views: 602

Answers (3)

splattne
splattne

Reputation: 104040

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

Upvotes: 7

yfeldblum
yfeldblum

Reputation: 65435

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

Upvotes: 8

user1228
user1228

Reputation:

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.

Upvotes: 20

Related Questions