In C# 3.0, I\'m liking this style:
// Write the numbers 1 thru 7
foreach (int index in Enumerable.Range( 1, 7 ))
{
Console.WriteLine(index);
}
I imagine there could be scenarios where Enumerable.Range(index, count)
is clearer when dealing with expressions for the parameters, especially if some of the values in that expression are altered within the loop. In the case of for
the expression would be evaluated based on the state after the current iteration, whereas Enumerable.Range()
is evaluated up-front.
Other than that, I'd agree that sticking with for
would normally be better (more familiar/readable to more people... readable is a very important value in code that needs to be maintained).