February 2010
Beginner
400 pages
11h 13m
English
ASP.NET MVC makes frequent use of type initializers, a feature added in C# version 3. The following is an example of this syntax:
public class person
{
public string FirstName{get;set;}
public string LastName{get;set;}
}
person Alex=new person{FirstName="Alex", LastName="Mackey"};
This is just a shorthand way to instantiate an object and set properties. In ASP.NET MVC, you will find this syntax frequently used when working with the HtmlHelper classes. For example, the following code generates a text box called LastName, bound to the LastName property of the model, and sizes the text box to 50 pixels.
<%= Html.TextBox("LastName", Model.LastName, new {@class="textbox", size=50} )%>
NOTE
Note the use of the @ sign as an ...