The Introduce ForEach Action refactoring
August 28th, 2012
1 comment
It is a rather trivial task to iterate over each item of the generic List using a foreach-loop as follows:
It is a rather trivial task to iterate over each item of the generic List using a foreach-loop as follows:
Let’s take a look at the two CodeRush refactorings allowing us to convert a for-loop into foreach-loop, and a foreach-loop into for-loop, and compare these two loops to see what the difference is between them and which one is preferred.
The for-loop is useful for iterating over elements and for sequential processing. It uses an iteration variable and is good when iterating over many items sequentially with a precise condition of the iteration termination. No collection of elements is required, but we can use the iteration variable to index a separate collection. In its basic form, the loop contains three sections: