How to Properly Repeat Characters in C#: From Naive Code to Optimization

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why the Question of Repeating a Character Became Iconic

\n\n

If you've ever written in C#, you've likely encountered the task: "I need to create a string consisting of N identical characters." It seems simple, right? But this very question on Stack Overflow has gathered thousands of views and dozens of answers. Why? Because behind the apparent simplicity lies a whole world of performance optimization and pitfalls.

\n\n

In this article, we won't just show you how to repeat a character in C#. We'll walk the path from a naive solution to professional techniques used in high-load systems. You'll learn which method is best suited for repeating a character in different scenarios, and why some popular advice is actually harmful.

\n\n

Who will this article be useful for?

\n
    \n
  • Beginner C# developers who want to write not just working, but efficient code
  • \n
  • Experienced programmers looking for ways to optimize string operations
  • \n
  • Anyone preparing for technical interviews who wants to impress with knowledge of .NET's internals
  • \n
\n\n

The Problem: Why You Shouldn't Write a for Loop

\n\n

When a beginner hears "repeat a character N times," the first thing that comes to mind is a for loop:

\n\n
string RepeatChar(char c, int count)\n{\n    string result = "";\n    for (int i = 0; i < count; i++)\n        result += c;\n    return result;\n}\n
\n\n

This solution works, but it is catastrophically slow. Why? Because strings in C# are immutable. Each += operation creates a new string, copying the old content plus one character into it. As a result, the complexity of this algorithm is O(n²). For 10 characters, it's unnoticeable, but for 100,000, it's an eternity.

\n\n

Let's look at real numbers. Let's test this method with count = 100,000. On a modern processor, this will take... several seconds. And if you need to repeat a character a million times? The program will simply "freeze."

\n\n

Conclusion: a loop with concatenation is evil. But there are other, more efficient approaches.

\n\n

Main Ways to Repeat a Character in C#

\n\n

Let's look at all the popular methods discussed on Stack Overflow and in real projects. We'll test each for performance and break down when to use which.

\n\n

1. Classic StringBuilder

\n\n

The first step to optimization is using StringBuilder. It doesn't create a new string on each addition, but works with a buffer:

\n\n
string RepeatWithStringBuilder(char c, int count)\n{\n    StringBuilder sb = new StringBuilder(count);\n    for (int i = 0; i < count; i++)\n        sb.Append(c);\n    return sb.ToString();\n}\n
\n\n

Notice: we immediately set the initial capacity to count. This is important! If you don't specify the capacity, StringBuilder will have to reallocate memory several times, which reduces performance.

\n\n

Performance: O(n). For 100,000 characters, this method executes in ~1-2 ms.

\n\n

2. Character Array + new string

\n\n

Now this is one of the fastest ways in .NET. Create an array of the required length, fill it with the character, and pass it to the string constructor:

\n\n
string RepeatWithArray(char c, int count)\n{\n    char[] chars = new char[count];\n    Array.Fill(chars, c);\n    return new string(chars);\n}\n
\n\n

Why is this fast? Because we work directly with memory: the array is filled in one pass, and the new string(char[]) constructor simply copies the data into the string's internal buffer. No extra operations.

\n\n

Performance:

Blogs

Book Recommendations