我有一系列结构。 foreach
运算符在通过数组迭代时是否复制每个元素?据我所知foreach
只是转化为for
的句法糖。所以答案似乎不是,但我很乐意得到一些确认。
答案 0 :(得分:8)
是的,将进行值类型实例的复制。迭代数组时,foreach
确实会使用数组访问而不是使用枚举器,但每个数组插槽中的值仍然会被复制。
此代码:
struct AStruct
{
public string a;
public int b;
static void Main()
{
var structs = new AStruct[10];
foreach (var x in structs) {
Console.WriteLine(x);
}
}
}
为Main()
方法生成以下IL:
.method private static hidebysig
default void Main () cil managed
{
.entrypoint
.maxstack 4
.locals init (
valuetype AStruct[] V_0,
valuetype AStruct[] V_1,
int32 V_2,
valuetype AStruct V_3)
IL_0000: ldc.i4.s 0x0a
IL_0002: newarr AStruct
IL_0007: stloc.0
IL_0008: ldloc.0
IL_0009: stloc.1
IL_000a: ldc.i4.0
IL_000b: stloc.2
IL_000c: br IL_002d
IL_0011: ldloc.1
IL_0012: ldloc.2
IL_0013: ldelema AStruct
IL_0018: ldobj AStruct
IL_001d: stloc.3
IL_001e: ldloc.3
IL_001f: box AStruct
IL_0024: call void class [mscorlib]System.Console::WriteLine(object)
IL_0029: ldloc.2
IL_002a: ldc.i4.1
IL_002b: add
IL_002c: stloc.2
IL_002d: ldloc.2
IL_002e: ldloc.1
IL_002f: ldlen
IL_0030: conv.i4
IL_0031: blt IL_0011
IL_0036: ret
} // end of method AStruct::Main
请注意IL_0013至IL_001d的说明。每个数组槽的整个值被压入堆栈并存储在本地V_3(x
迭代变量)中。