13.5. Type Initialization

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 escape character next to class to set the class property. The @ sign is used as an escape character because class is a keyword in .NET.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.41.235