Introduction: Why the Question of Repeating a Character Became Iconic
\n\nIf 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\nIn 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\nWho 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
The Problem: Why You Shouldn't Write a for Loop
\n\nWhen a beginner hears "repeat a character N times," the first thing that comes to mind is a for loop:
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\nThis 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.
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."
Conclusion: a loop with concatenation is evil. But there are other, more efficient approaches.
\n\nMain Ways to Repeat a Character in C#
\n\nLet'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\n1. Classic StringBuilder
\n\nThe first step to optimization is using StringBuilder. It doesn't create a new string on each addition, but works with a buffer:
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\nNotice: 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.
Performance: O(n). For 100,000 characters, this method executes in ~1-2 ms.
\n\n2. Character Array + new string
\n\nNow 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\nstring 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\nWhy 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.
Performance: