How to generate a sequence of numbers (for example: 3, 4, 5, 6) in C#

2 Answers

0 votes
using System;
using System.Linq;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Generate a range of 6 numbers starting at 5 
            IEnumerable <int> numbers = Enumerable.Range(3, 6);

            foreach (int n in numbers)
                Console.WriteLine(n);
        }
    }
}


/*
run:

3
4
5
6
7
8

*/

 



answered Mar 3, 2017 by avibootz
edited Mar 3, 2017 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Generate a range of -2 numbers starting at 5 
            IEnumerable <int> numbers = Enumerable.Range(3,-2);

            foreach (int n in numbers)
                Console.WriteLine(n);
        }
    }
}


/*
run:

Unhandled Exception: System.ArgumentOutOfRangeException: Specified argument was
out of the range of valid values.
Parameter name: count
   at System.Linq.Enumerable.Range(Int32 start, Int32 count)
   at ConsoleApplication_C_Sharp.Program.Main(String[] args) in d:\Conso
leApplication_C_Sharp\ConsoleApplication_C_Sharp\Program.cs:line 12

*/

 



answered Mar 3, 2017 by avibootz
...