C# 3.0 introduced object initializers, which ease construction and initialization of objects. An object initializer contains the values that the members of a newly created object should be assigned. It specifies values for one or more fields or properties of the object.
An object initializer has the following properties:
- It consists of a sequence of member initializers, enclosed by “{” and “}” tokens and separated by commas.
- Each member initializer can name only an accessible field or property of the object being initialized, followed by an equal sign (=) and an expression of the value (V) or an object initializer.
- A member initializer that specifies an expression of value (V), as above, is processed in the same way as an assignment to the field or property.
- A member initializer that specifies an object initializer after the equal sign is an initialization of an embedded object. The assignments in the object initializers are treated as assignments to members of the field or property. Value type properties cannot be initialized using this mechanism.
If an object-creation expression includes an object or collection initializer, it can omit the constructor argument list and enclosing parenthesis. Omitting the constructor argument list and the enclosing parenthesis is equivalent to specifying an empty argument list.
An object-creation expression is executed in two steps:
- Invoke an instance constructor, which creates the object.
- Perform member initialization using the values specified in the object initializers. (An object initializer cannot refer to the object being initialized.)
The object initializer first invokes the object’s parameterless constructor and then initializes the named fields to the specified values. Fields that are not specified will have the default values.
Example of Object Initializers
Suppose you have an entity Student, who can be identified by First Name and Last Name. You also have an entity ScienceClass, which is composed of three such students. This scenario can be represented with the following C# 3.0:
using System; using System.Collections.Generic; using System.Text; using System.Query; using System.Xml.XLinq; using System.Data.DLinq; namespace ObjectInitializers { class Program { public class Student { public string firstName; public string lastName; } public class ScienceClass { public Student Student1, Student2, Student3; } static void Main(string[] args) { var student1 = new Student{firstName = "Bruce", lastName = "Willis"}; var student2 = new Student{firstName = "George", lastName = "Clooney"}; var student3 = new Student{firstName = "James", lastName = "Cameron"}; var sClass = new ScienceClass{Student1 = student1, Student2 = student2, Student3 = student3}; } } }
If you have Visual Studio 2005 (any flavor) and the LINQ Preview installed, you can compile the above code in your IDE.
If you do not have VS 2005, but have the LINQ Preview installed, you can compile the code from the command line using the following command:
C:\Program Files\LINQ Preview\Bin\Csc.exe /reference:"C:\Program Files\LINQ Preview\Bin\System.Data.DLinq.dll" /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:"C:\Program Files\LINQ Preview\Bin\System.Query.dll" /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /reference:"C:\Program Files\LINQ Preview\Bin\System.Xml.XLinq.dll" Program.cs
Code Internals
Consider the following code snippet:
var student1 = new Student{firstName = "Bruce", lastName = "Willis"}; var student2 = new Student{firstName = "George", lastName = "Clooney"}; var student3 = new Student{firstName = "James", lastName = "Cameron"}; var sClass = new ScienceClass{Student1 = student1, Student2 = student2, Student3 = student3};
This snippet has the same effect to the compiler as the following:
var sClass = new ScienceClass(); var student1 = new Student(); student1.firstName = "Bruce"; student1.lastName = "Willis"; var student2 = new Student(); student2.firstName = "George "; student2.lastName = "Clooney"; var student3 = new Student(); student3.firstName = "James"; student3.lastName = "Cameron"; sClass.Student1 = student1; sClass.Student2 = student2; sClass.Student3 = student3;
If you fire up ILDASM and open up the compiled binary, you will see something like Figure 1.
Figure 1. Compiled Binary of Sample Code Snippet
If you double-click the Main node in the ILDASM, you will see the following code listing:
.method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 146 (0x92) .maxstack 2 .locals init ([0] class ObjectInitializers.Program/Student student1, [1] class ObjectInitializers.Program/Student student2, [2] class ObjectInitializers.Program/Student student3, [3] class ObjectInitializers.Program/ ScienceClass sClass, [4] class ObjectInitializers.Program/Student '<tampa>f__0', [5] class ObjectInitializers.Program/Student '<tampa>f__1', [6] class ObjectInitializers.Program/Student '<tampa>f__2', [7] class ObjectInitializers.Program/ScienceClass '<tampa>f__3') IL_0000: nop IL_0001: nop IL_0002: newobj instance void ObjectInitializers.Program/ Student::.ctor() IL_0007: stloc.s '<tampa>f__0' IL_0009: ldloc.s '<tampa>f__0' IL_000b: ldstr"Bruce" IL_0010: stfld string ObjectInitializers.Program/Student::firstName IL_0015: ldloc.s '<tampa>f__0' IL_0017: ldstr "Willis" IL_001c: stfld string ObjectInitializers.Program/Student::lastName IL_0021: ldloc.s '<tampa>f__0' IL_0023: nop IL_0024: stloc.0 IL_0025: nop IL_0026: newobj instance void ObjectInitializers.Program/ Student::.ctor() IL_002b: stloc.s '<tampa>f__1' IL_002d: ldloc.s '<tampa>f__1' IL_002f: ldstr "George" IL_0034: stfld string ObjectInitializers.Program/Student::firstName IL_0039: ldloc.s '<tampa>f__1' IL_003b: ldstr "Clooney" IL_0040: stfld string ObjectInitializers.Program/Student::lastName IL_0045: ldloc.s '<tampa>f__1' IL_0047: nop IL_0048: stloc.1 IL_0049: nop IL_004a: newobj instance void ObjectInitializers.Program/ Student::.ctor() IL_004f: stloc.s '<tampa>f__2' IL_0051: ldloc.s '<tampa>f__2' IL_0053: ldstr "James" IL_0058: stfld string ObjectInitializers.Program/Student::firstName IL_005d: ldloc.s '<tampa>f__2' IL_005f: ldstr Cameron" IL_0064: stfld string ObjectInitializers.Program/Student::lastName IL_0069: ldloc.s '<tampa>f__2' IL_006b: nop IL_006c: stloc.2 IL_006d: nop IL_006e: newobj instance void ObjectInitializers.Program/ ScienceClass::.ctor() IL_0073: stloc.s '<tampa>f__3' IL_0075: ldloc.s '<tampa>f__3' IL_0077: ldloc.0 IL_0078: stfld class ObjectInitializers.Program/ StudentObjectInitializers.Program/ScienceClass::Student1 IL_007d: ldloc.s '<tampa>f__3' IL_007f: ldloc.1 IL_0080: stfld class ObjectInitializers.Program/ Student ObjectInitializers.Program/ScienceClass::Student2 IL_0085: ldloc.s '<tampa>f__3' IL_0087: ldloc.2 IL_0088: stfld class ObjectInitializers.Program/ Student ObjectInitializers.Program/ScienceClass::Student3 IL_008d: ldloc.s '<tampa>f__3' IL_008f: nop IL_0090: stloc.3 IL_0091: ret } // end of method Program::Main