C# Programming
Character Repetition
Coding Techniques
String Manipulation
C# Tutorials

C#: repeat a character x times with new string(char, count)

Codemia Knowledge Hub ·

The simplest and fastest way to repeat a character in C# is the string constructor that takes a character and a count: new string('a', 10). Enumerable.Repeat and StringBuilder also work, but they only make sense when you are already using LINQ or building a larger string piece by piece.

new string(char, count)

csharp
1char charToRepeat = 'a';
2int count = 10;
3string repeatedString = new string(charToRepeat, count);
4// Output: aaaaaaaaaa

This is easy to read and highly efficient, because it is implemented natively in .NET. It has no significant drawbacks for general use, which makes it the right choice for padding, test data or console output.

Enumerable.Repeat with LINQ

Enumerable.Repeat produces a sequence with the given value repeated the specified number of times, which you then turn into a string:

csharp
1using System.Linq;
2
3char charToRepeat = 'b';
4int count = 5;
5string repeatedString = new string(Enumerable.Repeat(charToRepeat, count).ToArray());
6// Output: bbbbb

It can fit a LINQ-heavy codebase or a chain of other LINQ operations, but it is more verbose and slower than the constructor because of the LINQ overhead and the extra array.

StringBuilder in a loop

StringBuilder is the better fit when the string is built incrementally, or when the repeated character or segment varies as part of a larger manipulation:

csharp
1using System.Text;
2
3char charToRepeat = 'c';
4int count = 20;
5StringBuilder sb = new StringBuilder(count);
6for (int i = 0; i < count; i++)
7{
8    sb.Append(charToRepeat);
9}
10string repeatedString = sb.ToString();
11// Output: cccccccccccccccccccc

It is efficient when many appends are needed, but carries slightly more overhead than new string() for plain repetition.

Comparing the options

MethodUse CasePerformanceComplexity
new string()Direct and simple repetitionHighLow
Enumerable.RepeatWhen using LINQ workflowsMediumMedium
StringBuilderRepeated and/or complex manipulationsHighMedium

A few things to keep in mind:

  • Memory: very large strings have a real memory footprint and can affect performance.
  • Immutability: .NET strings are immutable, so methods that seem to modify a string actually create a new one. In performance-critical code, avoid creating strings you don't need.
  • Readability: prefer new string() for clarity and speed unless LINQ integration adds real value.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms