How to cook a loop

added by splove
7/26/2011 6:41:17 AM

205 Views

With coming of Linq we rarely write loops. Actually, the most part of data fetching tasks, those earlier was done by loops, today could be simply done by Linq-to-Objects. Nevertheless, the loops are widely used for different scenarios and a developer should try to write a good loop. This post was written for beginners, but I hope that experienced developers will find in it something useful.


1 comments

dpeterson
7/27/2011 12:20:15 AM
Some interesting talking points, particularly the interaction of loops and closures.
The only thing that irks me about the for loop example he used is that it seems much cleaner to step backwards through the array, rather than decrementing i when removing an item and fighting the loop. Simply change it to
for (var i = numbers.Count; i > 0; i--)
and you can simply remove the item from the array when you feel like it. :-)