Back to: LINQ Tutorial For Beginners and Professionals
LINQ Range Method in C# with Examples
In this article, I am going to discuss the LINQ Range Method in C# with Examples. Please read our previous article where we discussed How to Implement Paging using LINQ Skip and Take Methods in C# with an Example. The LINQ Range Method belongs to the Generation Operator Category. So let us first discuss what Generation Operator is and then we will see the Range method.
What is Generation Operator?
As of now, we have discussed so many LINQ Extension methods such as Select, Where, Any, etc. The Enumerable class also provides three Non-Extension Methods. They are as follows:
- Range()
- Repeat<T>()
- Empty<T>()
These three methods allow us to create some specific type of Array, Sequence, or Collection using a single expression instead of creating them manually and populating them using some kind of loop. That means these three methods return a new sequence or a new collection that implements the IEnumerable<T> interface.
Note: In this article, I am going to discuss the LINQ Range Method in C# with Examples. The rest two methods i.e. Repeat and Empty are going to discuss in our upcoming articles.
LINQ Range Method in C#:
The LINQ Range Method in C# is used to Generate a sequence of integral (integer) numbers within a specified range. The following is the signature of this method.
This method takes 2 integer parameters. The first parameter (i.e. int start) specifies the value of the first integer in the sequence. The second parameter (i.e. int count) specifies the number of sequential integers to be generated. The return type of this method is IEnumerable<int> which is going to contain a range of sequential integer numbers.
Note: When the count value is less than 0 or when the start + count – 1 value is larger than the System.Int32.MaxValue then it will through ArgumentOutOfRangeException.
Example to Understand LINQ Range Method in C#:
Let us see an example to understand the LINQ Range Method in C#. In the following example, we are generating a sequence of integer numbers starting from 1 to 10 using the Range Method.
using System.Linq; using System; using System.Collections.Generic; namespace LinqDemo { class Program { static void Main(string[] args) { //Generating Intger Numbers from 1 to 10 IEnumerable<int> numberSequence = Enumerable.Range(1, 10); //Accessing the numberSequence using Foreach Loop foreach (int num in numberSequence) { Console.Write($"{num} "); } Console.ReadKey(); } } }
Once you run the application, it will print the values from 1 to 10 as expected.
Example to Understand LINQ Range Method with Where Extension Method in C#:
Let us see an example to understand how to use LINQ Range Method with LINQ Where Extension Method. The following example prints all the even numbers which are present between 10 and 40. Here, first, we need to generate the sequence using the Range method and then we need to filter the data using the Where extension method.
using System.Linq; using System; using System.Collections.Generic; namespace LinqDemo { class Program { static void Main(string[] args) { //Using Range with Where Extension Method IEnumerable<int> EvenNumbers = Enumerable.Range(10, 40).Where(x => x % 2 == 0); //Printing the Even Numbers between 10 and 40 foreach (int num in EvenNumbers) { Console.Write($"{num} "); } Console.ReadKey(); } } }
Output:
Example to use Range Method with Select Extension Method in C#:
The following example shows how to generate a sequence of integers from 1 to 5 and then return the square of each number.
using System.Linq; using System; using System.Collections.Generic; namespace LinqDemo { class Program { static void Main(string[] args) { IEnumerable<int> EvenNumbers = Enumerable.Range(1, 5).Select(x => x*x); foreach (int num in EvenNumbers) { Console.Write($"{num} "); } Console.ReadKey(); } } }
Output: 1 4 9 16 25
Example to Understand LINQ Range Method with String Type
In the following example, first, we generate a sequence and then pass the sequence to our CustomLogic method and then merge the sequence with the return value from the CustomLogic method and return the result as IEnumerable<string>.
using System.Linq; using System; using System.Collections.Generic; namespace LinqDemo { class Program { static void Main(string[] args) { IEnumerable<string> rangewithString = Enumerable.Range(1, 5).Select(x => (x * x) + " " + CustomLogic(x)).ToArray(); foreach (var item in rangewithString) { Console.WriteLine(item); } Console.ReadKey(); } private static string CustomLogic(int x) { string result = string.Empty; switch (x) { case 1: result = "1st"; break; case 2: result = "2nd"; break; case 3: result = "3rd"; break; case 4: result = "4th"; break; case 5: result = "5th"; break; } return result; } } }
Output:
Note: This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using a for each loop.
In the next article, I am going to discuss the LINQ Repeat Method in C# with Examples. Here, in this article, I try to explain the LINQ Range Method in C# with some different types of examples. I hope you enjoy this article.
Linq Range Method in C#
Great article, you obviously possess technical communication skills!
It is so good that I enjoyed reading it from start to end.
Just found a small quirk the article. Fix this and it will be perfect!
Quote:
Example2: Range method with Filter.
The following print all the even numbers which are present between 10 and 30.
Issue
The present numbers are between 10 and 40.
Other than this very good stuff.
Kind regards,
gsoft
Hi,
Thanks for identifying the issue. We have corrected the example.