dcsimg
December 2, 2016
Hot Topics:

New in C# 3.0: Create and Initialize Collection Objects in One Step

  • May 19, 2006
  • By Vipul Patel
  • Send Email »
  • More Articles »

If you double-click the Main node in ILDASM, you will see the following code:

.method private hidebysig static void  Main(string[] args) cil managed
{
   .entrypoint
   // Code size       138 (0x8a)
   .maxstack  3
   .locals init ([0] class [mscorlib]System.Collections.Generic.List`1
                     <class CollectionInitializer.Program/MyClass>
                     classes,
                 [1] class [mscorlib]System.Collections.Generic.List`1
                     <class CollectionInitializer.Program/MyClass>
                     '<tampa>f__0',
                 [2] class CollectionInitializer.Program/MyClass
                     '<tampa>f__1',
                 [3] class CollectionInitializer.Program/MyClass
                     '<tampa>f__2')
   IL_0000:  nop
   IL_0001:  nop
   IL_0002:  newobj     instance void class [mscorlib]System.Collections.
                        Generic.List`1<class CollectionInitializer.
                        Program/MyClass>::.ctor()
   IL_0007:  stloc.1
   IL_0008:  ldloc.1
   IL_0009:  nop
   IL_000a:  newobj     instance void CollectionInitializer.
                        Program/MyClass::.ctor()
   IL_000f:  stloc.2
   IL_0010:  ldloc.2
   IL_0011:  ldstr      "Science"
   IL_0016:  stfld      string CollectionInitializer.
                        Program/MyClass::nameofClass
   IL_001b:  nop
   IL_001c:  ldloc.2
   IL_001d:  ldfld      class [mscorlib]System.Collections.Generic.List`1
                        <string> CollectionInitializer.
                        Program/MyClass::studentNames
   IL_0022:  ldstr      "Laura"
   IL_0027:  callvirt   instance void class [mscorlib]System.
                        Collections.Generic.List`1<string>::Add(!0)
   IL_002c:  nop
   IL_002d:  ldloc.2
   IL_002e:  ldfld      class [mscorlib]System.Collections.Generic.List`1
                        <string> CollectionInitializer.
                        Program/MyClass::studentNames
   IL_0033:  ldstr      "George"
   IL_0038:  callvirt   instance void class [mscorlib]System.Collections.
                        Generic.List`1<string>::Add(!0)
   IL_003d:  nop
   IL_003e:  nop
   IL_003f:  ldloc.2
   IL_0040:  nop
   IL_0041:  callvirt   instance void class [mscorlib]System.Collections.
                        Generic.List`1<class CollectionInitializer.
                        Program/MyClass>::Add(!0)
   IL_0046:  nop
   IL_0047:  ldloc.1
   IL_0048:  nop
   IL_0049:  newobj     instance void CollectionInitializer.
                        Program/MyClass::.ctor()
   IL_004e:  stloc.3
   IL_004f:  ldloc.3
   IL_0050:  ldstr      "Commerce"
   IL_0055:  stfld      string CollectionInitializer.
                        Program/MyClass::nameofClass
   IL_005a:  nop
   IL_005b:  ldloc.3
   IL_005c:  ldfld      class [mscorlib]System.Collections.Generic.List`1
                        <string> CollectionInitializer.
                        Program/MyClass::studentNames
   IL_0061:  ldstr      "Bill"
   IL_0066:  callvirt   instance void class [mscorlib]System.Collections.
                        Generic.List`1<string>::Add(!0)
   IL_006b:  nop
   IL_006c:  ldloc.3
   IL_006d:  ldfld      class [mscorlib]System.Collections.Generic.List`1
                        <string> CollectionInitializer.
                        Program/MyClass::studentNames
   IL_0072:  ldstr      "Hillary"
   IL_0077:  callvirt   instance void class [mscorlib]System.Collections.
                        Generic.List`1<string>::Add(!0)
   IL_007c:  nop
   IL_007d:  nop
   IL_007e:  ldloc.3
   IL_007f:  nop
   IL_0080:  callvirt   instance void class [mscorlib]System.Collections.
                        Generic.List`1<class CollectionInitializer.
                        Program/MyClass>::Add(!0)
   IL_0085:  nop
   IL_0086:  ldloc.1
   IL_0087:  nop
   IL_0088:  stloc.0
   IL_0089:  ret
}    // end of method Program::Main

As you can see from the two snippets prior to the above, C# 3.0 presents a significant improvement in language syntax.

One-Step Creation and Initialization for Object Collections

Collection initializers, one of the new features of C# 3.0, provide a new syntax to initialize collection objects. This simple syntax combines collection object creation and initialization into a single step.

Download the Code

You download the source code for this article here.

About the Author

Vipul Patel was a Microsoft MVP for two years. He specializes in C# and deployment issues. You can reach him at Vipul_d_patel@hotmail.com.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel