Give an example of LINQ Range Method.

Give an example of the LINQ Range Method.

Last updated:9/24/2021 3:18:15 AM

1 Answers

Ravi Vishwakarma
Ravi Vishwakarma

In LINQ, the Range() method is used to generate the list of integers or numbers based on specified values of start index and end index.

Syntax

Range(start_index, end_index)

Example

using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
 public static void Main()
 {
  List<int> range = Enumerable.Range(50,50).ToList();
  Console.WriteLine('Prime List ');
  range.ForEach(item => Console.Write('{0}', IsPrime(item) ? item+', ' : ''));
  Console.WriteLine('\b\b ');
 }
 public static bool IsPrime(int num){
  for(int i=2;i<Math.Sqrt(num);i++)
   if(num % i ==0 )
    return false;
 return true;
 }
}

Output

Prime List 
53, 59, 61, 67, 71, 73, 79, 83, 89, 97


Answer