源代码
object[] objs1 = new object[10];
foreach(object obj in objs1)
{
Console.WriteLine(obj.ToString());
}
编译后的代码
object[] objArray2 = objArray1;
for (int num2 = 0; num2 < objArray2.Length; num2++)
{
object obj1 = objArray2[num2];
Console.WriteLine(obj1.ToString());
}
源代码
object[] objs1 = new object[10];
for (int index = 0; index < objs1.Length; ++index)
{
Console.WriteLine(objs1[index].ToString());
}
编译后的代码
object[] objArray2 = objArray1;
for (int num1 = 0; num1 < objArray1.Length; num1++)
{
Console.WriteLine(objArray1[num1].ToString());
}
也就是说 foreach 会额外的产生一些代码,效率没有直接用 for 循环来的高
object[] objs1 = new object[10];
foreach(object obj in objs1)
{
Console.WriteLine(obj.ToString());
}
编译后的代码
object[] objArray2 = objArray1;
for (int num2 = 0; num2 < objArray2.Length; num2++)
{
object obj1 = objArray2[num2];
Console.WriteLine(obj1.ToString());
}
源代码
object[] objs1 = new object[10];
for (int index = 0; index < objs1.Length; ++index)
{
Console.WriteLine(objs1[index].ToString());
}
编译后的代码
object[] objArray2 = objArray1;
for (int num1 = 0; num1 < objArray1.Length; num1++)
{
Console.WriteLine(objArray1[num1].ToString());
}
也就是说 foreach 会额外的产生一些代码,效率没有直接用 for 循环来的高