Is foreach purely “syntactic sugar”?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 21:32:08

问题


The compiler compiles a foreach loop into something like a for loop when the foreach is used with an array. And the compiler compiles a foreach loop into something like a while loop when the foreach is used with an IEnumerable or IEnumerable<T>. So does this mean foreach is purely syntactic sugar? Or is there anything sophisticated about it?

Does the CLR know about foreach? Is there anything specifically designed for foreach in the MSIL code?


回答1:


It's purely syntactic sugar in that you could obtain the same behaviour without it, yes. Many other things are the same... for, while etc... To misquote Archimedes: "Give me if and goto, and I will move the code..."

No, the CLR doesn't have any concept of foreach.




回答2:


It is syntactic sugar. However, note that foreach works by calling GetEnumerator(), then MoveNext() until there is no further item returned and then always calls Dispose() on the enumerator it previously obtained. If you want to do it the same way, don't forget that Dispose()!

Also, the CLR does some tricks related to getting the enumerator. See here and here, for example.




回答3:


foreach is internally just a while loop that calls the methods in IEnumerator.




回答4:


It is not just syntactic sugar as the items in a foreach loop are immutable (unchangeable). The reason for this, as Daniel so kindly pointed out, is that most collections will use an enumerator in a foreach, and it is the enumerator that has the restriction of not letting you update the contents of the list while it is being enumerated.

i.e.

Foreach(String s in List<string>)
{
   s = "f";  //this will throw an error.
}



回答5:


Yes, it is purely sugar. The following code

var MyList = new List<int>() { 10 , 20 , 30 , 40, 50} ;  
foreach(int i in MyList) 
{
    Console.WriteLine(i);
}

is translated in compiler as:

Ienumrator<int> rator = MyList.GetEnumrator();

try
{
   while(rator.MoveNext())
   {
       int i = rator.Current; 
       Console.WriteLine(i); 
   }
}
finally
{
    rator.Dispose()
}


来源:https://stackoverflow.com/questions/5816776/is-foreach-purely-syntactic-sugar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!