C# Program to return a collection with repeated elements


To return a collection with repeated elements in C#, use Enumerable.Repeat method.

It is part of System.Linq namespace.

Let’s say you need to repeat a number twice, for that set the number and the frequency of repetition.

Enumerable.Repeat(50, 2);

Now assign it to a variable and display it.

Example

 Live Demo

using System;
using System.Linq;
class Demo {
   static void Main() {
      // repeating element 50, two times
      var num = Enumerable.Repeat(50, 2);
      // displayig repeating elements
      foreach (int ele in num) {
         Console.WriteLine(ele);
      }
   }
}

Output

50
50

Updated on: 23-Jun-2020

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements