How to create a sequence of integers in C#?

后端 未结 8 863
野的像风
野的像风 2020-11-30 02:32

F# has sequences that allows to create sequences:

seq { 0 .. 10 }

Create sequence of numbers from 0 to 10.

Is there someth

8条回答
  •  旧巷少年郎
    2020-11-30 03:34

    Linq projection with the rarely used indexer overload (i):

    (new int[11]).Select((o,i) => i)
    

    I prefer this method for its flexibilty.

    For example, if I want evens:

    (new int[11]).Select((item,i) => i*2)
    

    Or if I want 5 minute increments of an hour:

    (new int[12]).Select((item,i) => i*5)
    

    Or strings:

    (new int[12]).Select((item,i) => "Minute:" + i*5)
    

提交回复
热议问题