Enumerable.Range - When does it make sense to use?

后端 未结 5 995
-上瘾入骨i
-上瘾入骨i 2021-02-07 23:02

When programming it\'s almost instinctive deciding when to use a for loop, or foreach, but what is the determining factors or problem space for choosing to use Enumerable.Range?

5条回答
  •  爱一瞬间的悲伤
    2021-02-07 23:58

    Enumerable.Range() is a generator, i.e. it is a simple and powerfull way to generate n items of some sort.

    Need a collection with random number of instances of some class? No problem:

    Enumerable.Range(1,_random.Next())
        .Select(_ => new SomeClass
        {
            // Properties
        });
    

提交回复
热议问题