Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,950 questions

51,892 answers

573 users

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
...