Enumerable.Repeat method in C#

The Enumerable.Repeat method is part of the System.Linq namespace and creates a sequence that contains the same element repeated a specified number of times. This method is useful for initializing collections or generating test data with repeated values.

Syntax

Following is the syntax for the Enumerable.Repeat method −

public static IEnumerable<TResult> Repeat<TResult>(TResult element, int count)

Parameters

  • element − The value to be repeated in the resulting sequence.

  • count − The number of times to repeat the element.

Return Value

Returns an IEnumerable<TResult> that contains the specified element repeated the specified number of times.

Using Enumerable.Repeat with Numbers

The following example demonstrates how to repeat a number multiple times −

using System;
using System.Linq;

class Demo {
    static void Main() {
        var numbers = Enumerable.Repeat(10, 5);
        
        Console.WriteLine("Repeating number 10 five times:");
        foreach (int num in numbers) {
            Console.WriteLine(num);
        }
    }
}

The output of the above code is −

Repeating number 10 five times:
10
10
10
10
10

Using Enumerable.Repeat with Strings

The method works with any data type, including strings −

using System;
using System.Linq;

class Demo {
    static void Main() {
        var words = Enumerable.Repeat("Hello", 3);
        var chars = Enumerable.Repeat('*', 7);
        
        Console.WriteLine("Repeating string:");
        foreach (string word in words) {
            Console.WriteLine(word);
        }
        
        Console.WriteLine("\nRepeating character:");
        foreach (char c in chars) {
            Console.Write(c);
        }
        Console.WriteLine();
    }
}

The output of the above code is −

Repeating string:
Hello
Hello
Hello

Repeating character:
*******

Common Use Cases

The following example shows practical applications of Enumerable.Repeat

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

class Demo {
    static void Main() {
        // Initialize array with default values
        int[] defaultArray = Enumerable.Repeat(0, 5).ToArray();
        Console.WriteLine("Default array: [" + string.Join(", ", defaultArray) + "]");
        
        // Create list with repeated objects
        List<bool> flags = Enumerable.Repeat(true, 3).ToList();
        Console.WriteLine("Boolean flags: [" + string.Join(", ", flags) + "]");
        
        // Generate padding characters
        string padding = string.Concat(Enumerable.Repeat("-", 20));
        Console.WriteLine("Padding: " + padding);
    }
}

The output of the above code is −

Default array: [0, 0, 0, 0, 0]
Boolean flags: [True, True, True]
Padding: --------------------

Conclusion

The Enumerable.Repeat method provides an efficient way to create sequences with repeated elements. It is particularly useful for initializing collections with default values, generating test data, or creating padding patterns in string operations.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements