Briefly describe what an object initializer does and give a specific example. When is the object initializer assignment made?
What will be an ideal response?
An object initializer allows you to assign values to any accessible members or properties of a class at the time of instantiation without calling a constructor with parameters. For example, assuming an Employee class has been created with a public IdNumber property and a parameterless constructor, you can write an object initializer as follows:
Employee aWorker = new Employee {IdNumber = 101};
In this statement, 101 is assigned to the aWorker object's IdNumber property. The assignment is made within a pair of curly braces; no parentheses are used with the class name. When this statement executes, the parameterless, default constructor for the class is executed first, and then the object initializer assignment is made.
You might also like to view...
What type of load-balancing is utilized when team members are connected to a Hyper-V switch?
What will be an ideal response?
AES uses a 128-bit key and is used in PGP encryption software.
Answer the following statement true (T) or false (F)