Thoughts on foreach with Enumerable.Range vs traditional for loop

前端 未结 18 1089
花落未央
花落未央 2021-01-30 06:13

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);
}
18条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-30 07:08

    In my opinion the Enumerable.Range() way is more declarative. New and unfamiliar to people? Certainly. But I think this declarative approach yields the same benefits as most other LINQ-related language features.

提交回复
热议问题