There is now a shortcut for both declaring an instance of a class and setting the initial value of all or some of its members. With a single line of code, you can instantiate an object and set a number of properties on that object. During runtime, the object will first be created and then the properties will be set in the order in which they appear in the initialization list. This new feature is called object initializers.

Let’s look at an example. Suppose you have a class called Employee that has a number of properties like FirstName, LastName, FullName, Title, and the like. Using object initialization, you can both create an instance of this class and set the initial values of some (or all) of the Employee instance’s properties. To do so, you first construct the object. In Visual Basic, you follow this construction with the With keyword (C# does not require an equivalent indicator). You then place each property initialization inside a set of curly braces. Examples are as shown here:

C#

Employee emp = new Employee { FirstName = “Joe”,

LastName = “Smith”, Title = “Sr. Developer” };


VB

Dim emp As New Employee With {.FirstName = “Joe”, _

.LastName = “Smith”, .Title = “Sr. Developer”}

This single line of code is the equivalent of first creating an Employee class and then writing a line of code for each of the listed properties. Notice that in VB, you need to proceed the initialization using the With keyword; you also access each property using a dot. In C#, you do not need the dot or a keyword indicator.

Of course, you can also use object initialization with parameterized constructors. You simply pass the parameters into the constructor as you normally would. You then follow the constructor with the initialization. For example, suppose that the Employee class had a constructor that took the first and last name respectively. You could then create the object with the parameters and use object initialization for the Title as shown here:

C#

Employee emp = new Employee(“Joe”, “Smith”)

{ Title = “Sr. Developer” };


VB

Dim emp As New Employee(“Joe”, “Smith”) With _

{.Title = “Sr. Developer”}

Object initialization also allows you to write some code in the initialization. In addition, with VB you can use properties of the object you are initializing to help initialize other properties. This is not valid in C#. The C# compiler does not allow you to access the variable until the assignment is complete. To see an example of this, the following code initializes an Employee object and sets the Employee.FullName property by concatenating the first and last names. Notice that the VB code uses the object itself.

C#

Employee emp = new Employee { FirstName = “Joe”,

LastName = “Smith”, FullName = “Joe” + “ Smith”};


VB

Dim emp As New Employee() With {.FirstName = “Joe”, _

.LastName = “Smith”, _

.FullName = .FirstName & “ “ & .LastName}

You can also nest object initialization. That is, if a given property represents another object, you can create the other object as part of the initialization. You can also nest an initialization of the other object within the initialization of the first object. A simple example makes this clear. Suppose that the Employee class has a property called Location. The Location property may point to a Location object that includes the properties for City and State. You could then create the Employee object (along with the nested Location object) as shown here:

C#

Employee emp = new Employee { FirstName = “Joe”,

LastName = “Smith”, Location = new Location

{ City = “Redmond”, State = “WA” } };


VB

Dim emp As New Employee() With {.FirstName = “Joe”, _

.LastName = “Smith”, _

.Location = New Location With _

{.City = “Redmond”, .State = “Washington”}}


Source of Information : Sams Microsoft Visual Studio 2008 Unleashed

0 comments


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner