Is there an “upto” method in C#?

前端 未结 6 1788
攒了一身酷
攒了一身酷 2021-02-18 14:31

Here\'s a bit of code which prints out the squares of the numbers from 0 to 9:

for (int i = 0; i < 10; i++)
    Console.WriteLine(i*i);

Doin

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-18 14:42

    Take a look at LINQ TakeWhile or for your specific case of integers, use Enumerable.Range

    Enumerable.Range(1, 10).Select(i => ...);
    

    Arguably you shouldn't be putting an Action on the end there, see comments on ForEach here.

提交回复
热议问题