Enumerable.Repeat method in C#


Enumerable.Repeat method is part of System.Linq namespace.

It returns a collection with repeated elements in C#.

Firstly, set which element you want to repeat and how many times.

As an example, let us see how to repeat number 10, five times −

Enumerable.Repeat(10, 5);

The following is the complete example −

Example

 Live Demo

using System;
using System.Linq;
class Demo {
   static void Main() {
      var val = Enumerable.Repeat(10, 5);
      foreach (int res in val) {
         Console.WriteLine(res);
      }
   }
}

Output

10
10
10
10
10

Updated on: 22-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements