Give an example of the LINQ Repeat Method.

Give an example of the LINQ Repeat Method.

Last updated:9/24/2021 3:19:41 AM

1 Answers

Ravi Vishwakarma
Ravi Vishwakarma

The Repeat method is used to generate a collection or list with the same number as repeated times based on specified index values.

Syntax

Enumerable.Repeat<int>(start, times); 

Example

using System;

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

Output

List 

50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
Sum : 500


Answer