程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C# 3.0新特征之創建和初始化集合對象(2)

C# 3.0新特征之創建和初始化集合對象(2)

編輯:關於C語言

三、代 碼剖析

讓我們更細致地分析一下前面的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);

如果激活ILDASM並且打開了編譯的二進制代碼形式,那 麼你能夠看到如圖1類似的內容。

圖1.示例代碼片斷的編譯的二進制形式

如果你雙擊在ILDASM中的Main結點,那麼你會看到下列代碼:

.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方法結束

四、小結

從前面的一些代碼片斷中你可以看到,C# 3.0 在語法方面邁出了很大的一步。

集合初始化器,作為C# 3.0新引入的特 征之一,提供了一種新的語法來初始化集合對象。這種簡單的語法把集合對象的 創建和初始化結合到一個步驟中。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved