ForLoop & Foreach
-
They exist to service different needs. However, in some situations you can use both.
for(int i=0; i<array.Length; i++)
is faster for iterating over an array, but then you have to do more work as you have to extract the values out of the array manually.
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog -- modified at 4:34 Monday 29th May, 2006
-
They exist to service different needs. However, in some situations you can use both.
for(int i=0; i<array.Length; i++)
is faster for iterating over an array, but then you have to do more work as you have to extract the values out of the array manually.
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog -- modified at 4:34 Monday 29th May, 2006
Actually, Brad Abrams here[^] says that for arrays, for and foreach generate identical code for iteration.
Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Actually, Brad Abrams here[^] says that for arrays, for and foreach generate identical code for iteration.
Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Interesting - I've never check the MSIL but I've seen performance stats on code that suggest the for() is faster than foreach() for arrays - I guess it was talking about ArrayList and I didn't pick up on that.
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog