How to create a sequence of integers in C#?

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

    You could create a simple function. This would work for a more complicated sequence. Otherwise the Enumerable.Range should do.

    IEnumerable<int> Sequence(int n1, int n2)
    {
        while (n1 <= n2)
        {
            yield return  n1++;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 03:10
    Enumerable.Range(0, 11);
    

    Generates a sequence of integral numbers within a specified range.

    http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx

    0 讨论(0)
  • 2020-11-30 03:18

    I have these functions in my code

    private static IEnumerable<int> FromZero(this int count)
        {
            if (count <= 0)
                yield break;
    
            for (var i = 0; i < count; i++)
            {
                yield return i;
            }
        }
    
        private static IEnumerable<int> 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.

    0 讨论(0)
  • 2020-11-30 03:22

    You can use Enumerable.Range(0, 10);. Example:

    var seq = Enumerable.Range(0, 10);
    

    MSDN page here.

    0 讨论(0)
  • 2020-11-30 03:23

    My implementation:

        private static IEnumerable<int> Sequence(int start, int end)
        {
            switch (Math.Sign(end - start))
            {
                case -1:
                    while (start >= end)
                    {
                        yield return start--;
                    }
    
                    break;
    
                case 1:
                    while (start <= end)
                    {
                        yield return start++;
                    }
    
                    break;
    
                default:
                    yield break;
            }
        }
    
    0 讨论(0)
  • 2020-11-30 03:29

    Originally answered here.


    If you want to enumerate a sequence of numbers (IEnumerable<int>) from 0 to a variable end, then try

    Enumerable.Range(0, ++10);
    

    In explanation, to get a sequence of numbers from 0 to 10, you want the sequence to start at 0 (remembering that there are 11 numbers between 0 and 10, inclusive).


    If you want an unlimited linear series, you could write a function like

    IEnumerable<int> Series(int k = 0, int n = 1, int c = 1)
    {
        while (true)
        {
            yield return k;
            k = (c * k) + n;
        }
    }
    

    which you could use like

    var ZeroTo1000 = Series().Take(11);
    

    If you want a function you can call repeatedly to generate incrementing numbers, perhaps you want somthing like.

    using System.Threading;
    
    private static int orderNumber = 0;
    
    int Seq()
    {
        return Interlocked.Increment(ref orderNumber);
    }
    

    When you call Seq() it will return the next order number and increment the counter.

    0 讨论(0)
提交回复
热议问题