Updated on
The quickest way to turn a char[] into a string in C# is the string constructor, new string(charArray). string.Concat() and string.Join() give us the same string, but in our .NET 10 benchmark they were several times slower.
Let’s look at all three, and at why the gap exists.
The char type keyword is an alias for the .NET System.Char structure type, which represents a character as a UTF-16 code unit. It works the same way as string and System.String, which we cover in the difference between string and String in C#.
A char array holds text as a row of these characters. Each character can be accessed (and changed) without copying the rest of the characters, while a string’s characters can only be read. Microsoft Learn calls a String object immutable because “its value cannot be modified after it has been created”, so every change gives us a new string. When we build text piece by piece, a char array or a StringBuilder saves us those copies.
So, let’s say that we decided to use a char array for fast performance and we need a string object in the end. What are our options?
Creating Char Array
To convert char array to string in C#, first, we need to create a char[] object:
char[] charArray = ['c', 'o', 'd', 'e', ' ', 'm', 'a', 'z', 'e'];
Even though each member of the array holds one character, remember that a char in C# takes 2 bytes, one UTF-16 code unit. Our nine characters take 18 bytes, stored next to each other inside the array object on the managed heap, not on the stack. And one visible character isn’t always one char: an emoji such as 😀 takes two.
Using String Constructor
The first option to convert char array to string is to use a string constructor:
char[] charArray = ['c', 'o', 'd', 'e', ' ', 'm', 'a', 'z', 'e']; var convertedToString = new string(charArray); Console.WriteLine(convertedToString);
Once we start our application, it is going to print the result in the console window:
code maze
The constructor has an overload for when we need only part of the array. We pass the start index and the number of characters to take:
var partOfArray = new string(charArray, 0, 4); Console.WriteLine(partOfArray);
This time, the console shows only the first word:
code
Converting With String.Join() Method
Another option is using the string.Join method:
var joinedToString = string.Join("", charArray);
Console.WriteLine(joinedToString);
Again, after we start the application, we are going to see the same result.
So why would we ever pick string.Join()? For the separator. Anything other than an empty string goes between every pair of characters:
var withDashes = string.Join("-", charArray);
Console.WriteLine(withDashes);
Now the result has a dash between each character, including around the space:
c-o-d-e- -m-a-z-e
string.Join() and string.Concat() can do a lot more than this, and we cover both in different ways to concatenate strings in C#.
Using the String.Concat() Method
The last option is to use the string.Concat method:
var concatedToString = string.Concat(charArray); Console.WriteLine(concatedToString);
Of course, the result is going to be the same as with previous examples.
Which Is the Fastest Way to Turn a Char Array Into a String?
The string constructor is the fastest of the three, by a wide margin.
We measured all three on .NET 10 with BenchmarkDotNet. On our nine-character array, new string(charArray) was about four times faster than string.Concat() and string.Join(). On a 1,000-character array, it was about 20 times faster.
Why such a gap? The constructor knows the length up front. It allocates one string of exactly that size and copies the whole array into it in one block.
string.Concat() and string.Join() take an IEnumerable<char> instead. They walk the array one character at a time through an enumerator and append each one to a buffer. The enumerator is an object of its own, so they allocate a little more too.
That makes the constructor our default. We reach for string.Join() when we want a separator, and for string.Concat() when we have an IEnumerable<char> that isn’t an array.
Let’s see the benchmark we ran. The three methods sit side by side, and Length gives us a small and a large array:
[MemoryDiagnoser]
public class CharArrayToStringBenchmarks
{
private char[] _charArray = [];
[Params(9, 1000)]
public int Length { get; set; }
[GlobalSetup]
public void Setup() =>
_charArray = Enumerable.Range(0, Length).Select(i => (char)('a' + i % 26)).ToArray();
[Benchmark(Baseline = true)]
public string StringConstructor() => new string(_charArray);
[Benchmark]
public string StringConcat() => string.Concat(_charArray);
[Benchmark]
public string StringJoin() => string.Join("", _charArray);
}
The class goes in its own console project. We add the BenchmarkDotNet package to it, start the class file with using BenchmarkDotNet.Attributes;, and call BenchmarkRunner.Run<CharArrayToStringBenchmarks>(); from its Program.cs, after using BenchmarkDotNet.Running;. We run it in Release mode with BenchmarkDotNet, and these are the results:
| Method | Code | 9 chars | 1,000 chars | Allocated (9 chars) |
|---|---|---|---|---|
| String constructor | new string(charArray) | 9.4 ns | 95.5 ns | 40 B |
string.Concat() | string.Concat(charArray) | 37.9 ns | 1,884.2 ns | 72 B |
string.Join() | string.Join("", charArray) | 36.4 ns | 1,847.8 ns | 72 B |
So should we go and replace every string.Concat() in our code base? Probably not. A few dozen nanoseconds on a nine-character array is nothing. The gap matters when we convert large arrays, or convert many of them in a hot loop.
The three methods also differ on a null array. The constructor returns an empty string, while string.Concat() and string.Join() throw an ArgumentNullException.
How Do We Convert a Single Char to a String in C#?
A single char doesn’t need an array at all. Calling ToString() on it returns a one-character string, so 'c'.ToString() gives us "c".
When we want the same character repeated, the constructor has an overload for that too. new string('-', 3) returns "---", which comes in handy for separators and padding in console output.
String interpolation and the + operator work as well. Both $"{letter}" and "" + letter turn a letter variable of type char into the same one-character string, because C# converts the char for us.
Which one should we pick? ToString() says exactly what we mean, so it is our default. The other two make sense when the char is already part of a longer string we are building.
What about the opposite direction? Turning a string into a char or a char[] is a different job, with its own tools: ToCharArray(), char.Parse() and spans.
Conclusion
In this article, we have learned three ways to convert a char[] to a string in C#. When we change text one character at a time, a char array lets us do it in place, and one new string() call at the end gives us the string.
Array elements are stored together in one block of memory, so the constructor can copy them into the new string in one step. That is why it beat string.Concat() and string.Join() in our benchmark, and why it is our default.
For the opposite direction, see how to convert a string to char in C#.
Tested with .NET 10 (SDK 10.0.302), MSTest 4.5.1 and BenchmarkDotNet 0.15.8.
