Skip to main content

C# string and char extension methods to repeat the specified value x amount of times.

using System;

public static class Extensions
{
    public static string Repeat(this string self, int count)
    {
        return string.Concat(System.Linq.Enumerable.Repeat(self, count));
    }

    /// <inheritdoc cref="System.String(char, int)"/>
    public static string Repeat(this char character, int count)
    {
        return new String(character, count);
    }
}

//
// Usage

"-".Repeat(20); // --------------------

// Optimal... using char
'-'.Repeat(20); // --------------------