Create an Object and Initialize Its Values (Object Initializers)

There is 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 is created, and then the properties are set in the order in which they appear in the initialization list. This feature is called object initializers.

Let’s look at an example. Suppose you have a class called Employee that has a number of properties such as 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 Visual Basic, you access each property using a dot. In C#, you do not need the dot.

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 enables you to write some code in the initialization. In addition, with Visual Basic 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 Visual Basic 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 might 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"}}

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

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