LINQ (C#) – Range, Repeat and Empty Method with Programming Example

In this tutorial, you will learn:
  1. What is Range, Repeat and Empty Method in LINQ?
  2. Programming Example

Range Method

Range Method generates sequence of integral numbers within specified range. This Method generates sequential number based on starting point and ending point. You can specify starting point and ending point in the condition.

Define Range Method

public static IEnumerable<int> Range(int start, int count)

Programming Example

In this example, we will generate square number in sequence starting from 4 to 8 number.

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqTutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            IEnumerable<int> squareNumber = Enumerable.Range(4, 8).Select(x => x * x);
            foreach(var num in squareNumber)
            {   
                Console.WriteLine(num);
            }
            Console.ReadKey();
        }
    }
}

It is like:

  1. (x => x * x) is equal to (4 => 4 * 4)
  2. (x => x * x) is equal to (5 => 5 * 5)
  3. (x => x * x) is equal to (6 => 6 * 6)
  4. (x => x * x) is equal to (7 => 7 * 7)
  5. (x => x * x) is equal to (8 => 8 * 8)
  6. (x => x * x) is equal to (9 => 9 * 9)
  7. (x => x * x) is equal to (10 => 10 * 10)
  8. (x => x * x) is equal to (11 => 11 * 11)

Output:


16
25
36
49
64
81
100
121
_

Understand the Code

IEnumerable<int> squareNumber = Enumerable.Range(4, 8).Select(x => x * x);

  1. We created an Enumerable Integer variable squareNumber that will hold the list of returned integer value.
  2. In Range(4, 8): It will start from 4 and will count the values 8 times.
  3. Select (x => x * x) is a condition where we can define how the value will be calculated. In this condition, each time value will be multiplied by itself and print square value.

More Condition Example

Random r = new Random();
IEnumerable<int> squareNumber = Enumerable.Range(4, 8).Select(x => x + r.Next(x));

In the above condition, each time the value will add a random number and then display the output. Each time when you will run the program, you will get different output each time.

It is like:

9. (x => x + r.Next(x) is equal to (4 => 4 + RandomNumber)
10. (x => x + r.Next(x) is equal to (5 => 5 + RandomNumber)
11. (x => x + r.Next(x) is equal to (6 => 6 + RandomNumber)
12. (x => x + r.Next(x) is equal to (7 => 7 + RandomNumber)
13. (x => x + r.Next(x) is equal to (8 => 8 + RandomNumber)
14. (x => x + r.Next(x) is equal to (9 => 9 + RandomNumber)
15. (x => x + r.Next(x) is equal to (10 => 10 + RandomNumber)
16. (x => x + r.Next(x) is equal to (11 => 11 + RandomNumber)

Output

5
7
8
11
9
11
15
20
_

Repeat Method

Repeat methods just repeats the integer value on the given number of times. For example, if we need to repeat integer value 35 into 10 times then we can write it as follow:

IEnumerable<int> repeatNumber = Enumerable.Repeat(35, 10);

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqTutorial
{  
    class Program
    {
        static void Main(string[] args)
        {
            IEnumerable<int> repeatNumber = Enumerable.Repeat(35, 10);
            foreach(var num in repeatNumber)
            {   
                Console.WriteLine(num);
            }
            Console.ReadKey();
        }
    }
}

Output

35
35
35
35
35
35
35
35
35
35
_

Empty Method

Empty method returns an empty collection or empty sequence.

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqTutorial
{  
    class Program
    {
        static void Main(string[] args)
        {
            var emptyNumber = Enumerable.Empty<string>();
            foreach(var num in emptyNumber)
            {   
                Console.WriteLine(num);
            }
            Console.ReadKey();
        }
    }
}

Summary

In this tutorial, you learned Range(), Repeat() and Empty() method in LINQ with the help of complete programming example in c#. In the next chapter, you will learn How to use LINQ to collect data with different types of .Net Collection.

More Articles

 

Share your thought



PLEASE DISABLE ADBLOCKER AND SUPPORT US!

Please support us by enabling ads on this page. Refresh

YOU DON'T LIKE ADS, WE ALSO DON'T LIKE ADS!
 
But we have to show ads on our site to keep it free and updated. We have to pay huge server costs, domain costs, CDN Costs, Developer Costs, Electricity and Internet Bill. Your little contribution will encourage us to regularly update this site.