列表<;int>;C#3.5中的初始化
我可以像新列表{1,2,3,4,5}一样初始化列表<;int>;C#3.5中的初始化,c#,.net-3.5,C#,.net 3.5,我可以像新列表{1,2,3,4,5}一样初始化列表 但是,List没有接受单个参数的构造函数。 所以我尝试通过调试器运行它,它似乎在调用Add方法。 那么编译器如何知道调用哪个方法来添加每个元素呢 这可能是一个愚蠢的问题,但我有点困惑 谢谢每个具有Add方法并实现IEnumerable的类型都可以通过这种方式初始化。 编译器只是编译您的代码,就像您使用这个Add方法一样 看看新列表{1,2,3,4,5}只是语法上的“糖”。在封面下,它只需为每个项目调用Add方法。看看这个方法 publi
列表代码>
但是,List
没有接受单个参数的构造函数。
所以我尝试通过调试器运行它,它似乎在调用Add方法。
那么编译器如何知道调用哪个方法来添加每个元素呢
这可能是一个愚蠢的问题,但我有点困惑
谢谢每个具有Add方法并实现IEnumerable的类型都可以通过这种方式初始化。
编译器只是编译您的代码,就像您使用这个Add方法一样
看看新列表{1,2,3,4,5}
只是语法上的“糖”。在封面下,它只需为每个项目调用Add
方法。看看这个方法
public void CreateList()
{
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
}
public void CreateList()
{
列表=新列表{1,2,3,4,5};
}
编译后,MSIL如下所示
.method public hidebysig instance void CreateList() cil managed
{
// Code size 50 (0x32)
.maxstack 2
.locals init ([0] class [mscorlib]System.Collections.Generic.List`1<int32> list,
[1] class [mscorlib]System.Collections.Generic.List`1<int32> '<>g__initLocal0')
IL_0000: nop
IL_0001: newobj instance void class [mscorlib]System.Collections.Generic.List`1<int32>::.ctor()
IL_0006: stloc.1
IL_0007: ldloc.1
IL_0008: ldc.i4.1
IL_0009: callvirt instance void class [mscorlib]System.Collections.Generic.List`1<int32>::Add(!0)
IL_000e: nop
IL_000f: ldloc.1
IL_0010: ldc.i4.2
IL_0011: callvirt instance void class [mscorlib]System.Collections.Generic.List`1<int32>::Add(!0)
IL_0016: nop
IL_0017: ldloc.1
IL_0018: ldc.i4.3
IL_0019: callvirt instance void class [mscorlib]System.Collections.Generic.List`1<int32>::Add(!0)
IL_001e: nop
IL_001f: ldloc.1
IL_0020: ldc.i4.4
IL_0021: callvirt instance void class [mscorlib]System.Collections.Generic.List`1<int32>::Add(!0)
IL_0026: nop
IL_0027: ldloc.1
IL_0028: ldc.i4.5
IL_0029: callvirt instance void class [mscorlib]System.Collections.Generic.List`1<int32>::Add(!0)
IL_002e: nop
IL_002f: ldloc.1
IL_0030: stloc.0
IL_0031: ret
} // end of method Program::CreateList
.method public hidebysing实例void CreateList()cil managed
{
//代码大小50(0x32)
.maxstack 2
.locals init([0]类[mscorlib]系统.Collections.Generic.List`1列表,
[1] 类[mscorlib]System.Collections.Generic.List“1”g\uu initLocal0')
IL_0000:没有
IL_0001:newobj实例无效类[mscorlib]System.Collections.Generic.List`1::.ctor()
IL_0006:stloc.1
IL_0007:ldloc.1
IL_0008:ldc.i4.1
IL_0009:callvirt实例无效类[mscorlib]System.Collections.Generic.List`1::Add(!0)
没有
IL_000f:ldloc.1
IL_0010:ldc.i4.2
IL_0011:callvirt实例无效类[mscorlib]System.Collections.Generic.List`1::Add(!0)
IL_0016:没有
IL_0017:ldloc.1
IL_0018:ldc.i4.3
IL_0019:callvirt实例无效类[mscorlib]System.Collections.Generic.List`1::Add(!0)
IL_001e:没有
IL_001f:ldloc.1
IL_0020:ldc.i4.4
IL_0021:callvirt实例无效类[mscorlib]System.Collections.Generic.List`1::Add(!0)
IL_0026:没有
IL_0027:ldloc.1
IL_0028:ldc.i4.5
IL_0029:callvirt实例无效类[mscorlib]System.Collections.Generic.List`1::Add(!0)
IL_002e:没有
IL_002f:ldloc.1
IL_0030:stloc.0
IL_0031:ret
}//方法程序结束::CreateList
正如您所看到的,它只是一个语法糖,编译器通过连续调用Add()来替换初始化 这是一个集合初始值设定项,一个。它要求:
- 该类型必须实现
IEnumerable
(尽管它从未用于初始化)
- 该类型必须至少有一个
Add
方法
它只是为每个术语调用Add
方法。
如果Add
接受多个值(例如字典),也可以使用元组。然后,每个术语都是{key,value}
:
new Dictionary<int,string> {{1,"abc"},{2,"def"}};
由于新列表{1,2,3,4,5}
是数组初始化,我更倾向于认为它是通过魔法在内部实现的,因为我无法影响它的发生方式。仔细想想,它可能定义了向元数据集合中添加元素的默认方法,其中[]
是通过索引完成的,而实现IList
的元素是通过Add
方法完成的。不完全是这样-它还必须实现IEnumerable(Add方法必须有参数,并且必须有一个可访问的构造函数)。At不是“数组初始化”——这是一个工作方式非常不同的单独主题。而且碰巧它也不依赖于IList.Add
(但这是一个合理的假设)我的意思是说集合初始化,这也可能有助于OP hmmm,没有人会拒绝投票给正确答案。民主在起作用!
class Program
{
static void Main()
{
new Foo { 1, "abc", { 2, "def" } };
}
}
class Foo : IEnumerable
{
public void Add(int a) { }
public void Add(string b) { }
public void Add(int a, string b) { }
// must implement this!! (but never called)
IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); }
}