http://www.developer.com/net/csharp/article.php/3605011/One-Step-Object-Creation-and-Initialization-in-C-30.htm
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: 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: 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. 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: 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: Consider the following code snippet: This snippet has the same effect to the compiler as the following: 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: You can also have embedded initialization in ScienceClass, but you will need to declare your ScienceClass a little differently, as shown below: Object initializers provide a new syntax to initialize the objects you create. This simple syntax combines object creation and initialization into a single step. You download the source code for this article here. Vipul Patel is a Microsoft MVP (two years in a row) in Visual C# and currently works at Microsoft through Volt Information Sciences. He specializes in C# and deployment issues. You can reach him at Vipul_d_patel@hotmail.com.
One-Step Object Creation and Initialization in C# 3.0
May 10, 2006
Example of Object Initializers
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};
}
}
}
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
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};
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;

Click here for a larger image. .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
public class ScienceClass
{
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
public Student Student1
{
get
{
return s1;
}
set
{
s1 = value;
}
}
public Student Student2
{
get
{
return s2;
}
set
{
s2 = value;
}
}
public Student Student3
{
get
{
return s3;
}
set
{
s3 = value;
}
}
}
One-Step Object Creation and Initialization
Download the Code
About the Author