public class Student{ public string firstName; public string lastName; } |
var student1 = new Student{firstName = "Bruce", lastName = "Willis"}; |
List<string> names = new List<string> { "Alice", "Bob", "Chris" }; |
using System; using System.Collections.Generic; using System.Text; using System.Query; using System.Xml.XLinq; using System.Data.DLinq; namespace CollectionInitializer { class Program { public class MyClass { public string nameofClass; public List<string> studentNames = new List<string>(); } static void Main(string[] args) { var classes = new List<MyClass> { new MyClass { nameofClass = "Science", studentNames = {"Laura", "George"} }, new MyClass { nameofClass = "Commerce", studentNames = {"Bill", "Hillary"} } }; } } } |
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 |
精品教程尽在www.xvna.com
三、 代码剖析
让我们更细致地分析一下前面的C# 3.0代码:
var classes = new List<MyClass> { new MyClass { nameofClass = "Science", studentNames = {"Laura", "George"} }, new MyClass { nameofClass = "Commerce", studentNames = {"Bill", "Hillary"} } }; |
var classes = new List<MyClass>(); var __c1 = new MyClass (); __c1.nameofClass = "Science"; __c1.studentNames.Add("Laura"); __c1.studentNames.Add("George"); classes.Add(__c1); var __c2 = new MyClass(); __c2.nameofClass = "Commerce"; __c2.studentNames.Add("Bill"); __c2.studentNames.Add("Hillary"); classes.Add(__c2); |
图1.示例代码片断的编译的二进制形式 |
.method private hidebysig static void Main(string[] args) cil managed { .entrypoint //代码大小 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 } //Program::Main方法结束 |