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)
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:
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:
It is efficient when many appends are needed, but carries slightly more overhead than new string() for plain repetition.
Comparing the options
| Method | Use Case | Performance | Complexity |
new string() | Direct and simple repetition | High | Low |
Enumerable.Repeat | When using LINQ workflows | Medium | Medium |
StringBuilder | Repeated and/or complex manipulations | High | Medium |
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.
Related reading
- Best way to store encryption keys in .NET C
- Best way to turn an integer into a month name in c?
- Best way to update LINQ to SQL classes after database schema change
- Bidirectional 1 to 1 Dictionary in C
- Bind TextBox on Enter-key press
- Bind to a method in WPF?
- Binding an enum to a WinForms combo box, and then setting it
- Binding ItemsSource of a ComboBoxColumn in WPF DataGrid

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.