LINQ Range Method

  Total Views : 131
  Comments : 0
 
  Keywords : Linq range operator in c# / vb.net with example, How to use linq range method in c# / vb.net to generate sequence of numbers based on specified index values with example

Here we will learn LINQ Range operator or method in c#, vb.net with example and how to use LINQ Range method to generate sequence of integers or numbers in c#, vb.net with example.

LINQ Range Method

In LINQ range method or operator is used to generate sequence of integers or numbers based on specified values of start index and end index. 

Syntax of LINQ Range Method

Following is the syntax of LINQ Range method to generate sequence of numbers based on specified index values.

 

C# Code

 

IEnumerable<int> obj = Enumerable.Range(100, 10);

VB.NET Code

 

Dim obj As IEnumerable(Of Integer) = Enumerable.Range(100, 10)

If you observe above syntax we defined Range method with two parameters. Here first parameter tells the starting elements of integer and the second parameter is the one that tells upto which limit it display the integers in a sequence.

Example of LINQ Range Method

Following is the example of LINQ Range method to generate a collection that contains sequence of numbers.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace Linqtutorials

{

class Program

{

static void Main(string[] args)

{

IEnumerable<int> obj = Enumerable.Range(100, 10);

foreach (var item in obj)

{

Console.WriteLine(item);

}

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim obj As IEnumerable(Of Integer) = Enumerable.Range(100, 10)

For Each item In obj

Console.WriteLine(item)

Next

Console.ReadLine()

End Sub

End Module

If you observe above example we defined Range from (100, 10) so it will take starting number as ”100” and till “109” it will return because here we defined second parameter as 10 so it will return 10 numbers.

Output LINQ Range Method Example

Following is the result of LINQ Range method example.

 

100

101

102

103

104

105

106

107

108

109

This is how we can use LINQ Range method to generate collection with sequence of numbers in c#, vb.net.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User