Back to Blog
C#

Converting a Char Array to a String in C#

c# char array to string: Learn the practical ways to convert a char array to a string in C#, including new string(), String.Concat, and StringBuilder, with performance...

C#char arraystring conversionString.ConcatStringBuilder
Illustration of a C# char array being converted into a string, showing the transformation from individual characters to a single string object.

c# char array to string requires a clear understanding of the core syntax, runtime behavior, and practical implementation patterns demonstrated in the examples below.

Converting a char[] to a string is a common operation in C#. The most direct way is to use the string constructor that accepts a char[], but other approaches exist and each has its own tradeoffs. This article covers the main conversion methods, their runtime behavior, and the conditions that should guide your choice.

The Direct Conversion with new string(char[])

The string class provides a constructor that takes a char[] and creates a new string from its contents. This is the simplest and most efficient way to perform the conversion when you already have the complete array.

char[] chars = { 'H', 'e', 'l', 'l', 'o' }; string result = new string(chars); Console.WriteLine(result); // Output: Hello

The constructor copies the characters into the internal string buffer. Because strings are immutable, the resulting string is independent of the original array. If you modify chars after creating the string, the string remains unchanged. This behavior is important when the array is reused later.

This approach works in all .NET versions and is the idiomatic way to perform a one-time conversion. It also handles an empty array correctly, producing string.Empty.

Using string.Concat and string.Join

The static string.Concat method has an overload that accepts a char[] and returns a string. Internally it calls the same constructor, so the result is identical for a complete array.

char[] chars = { 'W', 'o', 'r', 'l', 'd' }; string result = string.Concat(chars);

string.Join can also convert a char[] if you provide a separator, but that adds the separator between each character, which is rarely what you want for a direct conversion. For example, string.Join("", chars) works but is less efficient because it goes through a join routine. Prefer new string(chars) or string.Concat(chars) when no separator is needed.

When to Use StringBuilder

If you are building a string incrementally from characters, StringBuilder is the appropriate tool. It is not designed for a one-time conversion from an existing array, but it becomes relevant when you append characters in a loop and then convert the result.

char[] chars = { 'a', 'b', 'c' }; var builder = new StringBuilder(); foreach (char c in chars) { builder.Append(c); } string result = builder.ToString();

This is more verbose and slower than the direct constructor for a complete array. Use StringBuilder only when you are assembling a string over multiple steps, such as parsing a stream or building a message piece by piece. For a simple array-to-string conversion, it adds unnecessary overhead.

Performance and Memory Behavior

The new string(char[]) constructor performs a single allocation for the string and copies the characters. The time is proportional to the array length. string.Concat(char[]) does the same thing internally, so there is no practical performance difference between the two for a complete array.

string.Join with an empty separator goes through extra steps to handle separators and enumeration, even though the result is identical. If you are converting a large array frequently, new string(char[]) is the most direct path and avoids the overhead of a join operation.

StringBuilder allocates internal buffers that grow as you append. For a one-time conversion, that means additional allocations and copying compared to the direct constructor. If you already have the full array, there is no reason to use StringBuilder for the conversion itself.

One subtle point: the string constructor copies the array contents. If you need to avoid that copy, you can use ReadOnlySpan<char> with the string constructor that accepts a span, available in .NET Core 2.1 and later. That still creates a string, but it avoids an intermediate array if you already have a span. For most scenarios, the standard constructor is sufficient.

Handling Null, Empty, and Large Arrays

Passing a null array to new string(char[]) throws ArgumentNullException. If you have a method that may receive null, check for it explicitly or use the null-coalescing operator to provide a default.

char[]? chars = GetChars(); string result = chars == null ? string.Empty : new string(chars);

An empty array produces string.Empty, which is a cached instance, so no new allocation occurs. This is a convenient property when you often deal with empty inputs.

For very large arrays, the conversion allocates a string of the same length, so memory usage doubles temporarily. If the array is no longer needed after conversion, you can let it go out of scope to allow garbage collection. If you are converting many large arrays in a loop, consider reusing buffers or using Span<char> to reduce allocations, but only if profiling shows a real problem.

Choosing the Right Approach for Your Scenario

The decision depends on whether you already have the complete array and whether you need to build the string incrementally.

  • Use new string(char[]) when you have a complete char[] and want a one-time conversion. It is the clearest and most efficient option.
  • Use string.Concat(char[]) if you prefer a static method call, but the result is identical to the constructor.
  • Use StringBuilder when you are appending characters over time and only need the final string at the end.
  • Avoid string.Join for this purpose unless you actually need a separator between characters.

A practical scenario where this matters is reading a character buffer from a stream or a network socket. If the buffer is already filled, new string(buffer, startIndex, count) lets you convert a portion of the array without copying the whole thing. This overload is useful when the array is larger than the actual data.

char[] buffer = new char[1024]; int read = ReadFromStream(buffer); string content = new string(buffer, 0, read);

This avoids allocating a temporary array for the exact length and is the preferred way to handle partial buffers. The same overload exists on string.Concat? No, string.Concat does not have a range overload, so the constructor is the right choice here.

Understanding these conversion paths helps you write code that is both clear and efficient, especially when working with character data at the boundaries of your application.

c# char array to string: Practical Usage and Code Examples | RYUSLOG DEV