CSharp - Repeat for PLINQ


The Repeat operator creates a ParallelQuery<T> that contains a single value of type T repeated a specified number of times.

Repeat operator is a static method in the ParallelEnumerable class, rather than an extension method operator.


public static ParallelQuery<T> Repeat<T>(
          T element,
          int count

The Repeat method takes two arguments.

The first is the element that you want to repeat.

The second is the number of times that the element should be repeated in the sequence.

The following code demonstrates creating a repeating sequence using the Repeat method.

We create a sequence where the integer value 2 is repeated 10 times.

Using a foreach loop, we enumerate the sequence and print out each element in the sequence.


using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

class Program/*w w  w  .j a v a 2s. c  om*/
    static void Main(string[] args)
          ParallelQuery<int> pq = ParallelEnumerable.Repeat(2, 10);
          foreach (int i in pq) {
              Console.WriteLine("Value {0}", i);