C# Program to return a collection with repeated elements

CsharpProgrammingServer Side Programming

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
raja
Published on 12-Sep-2018 12:50:52
Advertisements