Dictionary<string, int> table = new Dictionary<string, int>
{
{ "Kelvin", 24 },
{ "Catherine", 24 },
{ "Aden", 1000 },
};
實際上就是由編譯器來調用Add方法(所以如果沒實現Add方法,就不可以這樣初始化)
IL_004f: newobj instance void class [System.Collections]System.Collections.Generic.Dictionary`2<string,int32>::.ctor()
IL_0054: dup
IL_0055: ldstr "Kelvin"
IL_005a: ldc.i4.s 24
IL_005c: callvirt instance void class [System.Collections]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0061: nop
IL_0062: dup
IL_0063: ldstr "Catherine"
IL_0068: ldc.i4.s 24
IL_006a: callvirt instance void class [System.Collections]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_006f: nop
IL_0070: dup
IL_0071: ldstr "Aden"
IL_0076: ldc.i4 0x3e8
IL_007b: callvirt instance void class [System.Collections]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)