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 );
}
over the traditional for loop:
// Write the numbers 1 thru 7
for( int index = 1; index <= 7; index++ )
{
Console.WriteLine( index );
}
Assuming 'n' is small so performance is not an issue, does anyone object to the new style over the traditional style?
Update: Thanks to everyone for their input! I think the main points are:
- The traditional 'for' loop is well understood, and offers the least surprise to other developers reading your code.
- Enumerable.Range would be better if it was shorter
- Enumerable.Range would be better if the parameters were (start, end) instead of (start, count)
- And if you really want to go to town, you can use an extension method to get "1.To(7) syntax"