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

后端 未结 5 993
-上瘾入骨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:46

    foreach is about iterating over an existing set/collection.

    Enumerable.Range is for generating a set/collection. You wouldn't, generally, want to write a for loop just to generate a set if it can be generated by Enumerable.Range - you'd just be writing boilerplate code that's longer and requires you to allocate some kind of storage (e.g. a List) to populate first.

提交回复
热议问题