How to create a sequence of integers in C#?

后端 未结 8 857
野的像风
野的像风 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:18

    I have these functions in my code

    private static IEnumerable FromZero(this int count)
        {
            if (count <= 0)
                yield break;
    
            for (var i = 0; i < count; i++)
            {
                yield return i;
            }
        }
    
        private static IEnumerable FromOne(this int count)
        {
            if (count <= 0)
                yield break;
    
            for (var i = 1; i <= count; i++)
            {
                yield return i;
            }
        }
    

    This helps to reduce some for(i) code.

提交回复
热议问题