Updated on
To convert a string array to a string in C#, we call string.Concat(array), or string.Join(separator, array) when we want something between the elements. Calling array.ToString() won’t work, because it returns the name of the type, System.String[].
We will cover five different approaches to achieve the same result, and in the end, we will inspect benchmark results on .NET 11 to see the fastest way to accomplish the conversion.
Let’s start.
Convert Using Loop and Addition Assignment Operator
We keep all our conversion methods in an ArrayConverter class. The first and easiest way to convert a string array into a string is using the addition assignment += operator:
public string UsingLoopStringAdditionAssignment(string[] array)
{
var result = string.Empty;
foreach (var item in array)
{
result += item;
}
return result;
}
First, we create an empty string variable result to represent the final result. In the next step, we loop through the array and increment the result variable with each element inside the array. Then, we return the result variable containing every array’s element.
Even though it is an easy-to-implement approach, it is not a good one. Since the string is an immutable type, in each iteration, we copy the entire string content and add the new value to the result variable. The Microsoft Learn guide to StringBuilder explains that every time we use one of the String methods, we “create a new string object in memory, which requires a new allocation of space for that new object.” It is also good to know that we can optimize some operations on single strings (like the Substring) using Span.
That said, we are going to implement our second approach using a loop. Let’s do it.
Convert String Array to String Using Loop and StringBuilder
This approach is very similar to the previous one, with the exception that, this time, we are going to use the StringBuilder class instead of a string. Let’s create a UsingLoopStringBuilder method to accomplish this:
using System.Text;
public string UsingLoopStringBuilder(string[] array)
{
var result = new StringBuilder();
foreach (var item in array)
{
result.Append(item);
}
return result.ToString();
}
First, we instantiate a StringBuilder object to a result variable. Then, inside the loop, we append each element to it. In the end, we return the string inside the result variable using the result.ToString().
Convert String Array to String Using String.Join
Let’s convert a string array into a string using a string.Join(...) method:
public string UsingStringJoin(string[] array)
{
return string.Join(string.Empty, array);
}
The string class contains a static Join(...) method, and with it, we can accomplish the same result.
The first input parameter of the string.Join(...) method represents a separator used between each element. Since we don’t need any separator in our example, we use a string.Empty to represent an empty string. However, we could use any char, string, or even a white space to separate the elements. The second parameter represents the array of the elements we want to convert into a string.
Behind the scenes, the string.Join(...) method doesn’t use a StringBuilder. It adds up the lengths of all the elements and separators first, allocates the result string once, and copies every element into it.
Convert Using String.Concat
Let’s create a UsingStringConcat method to convert a string array into a string:
public string UsingStringConcat(string[] array)
{
return string.Concat(array);
}
First, our method receives the array we want to convert.
Then we call the string‘s static Concat(...) method. This method works similarly to the string.Join(...). Yet, it doesn’t receive any separator, but it is perfect for achieving the results we want in this article. However, if we need to have a separator between elements, we need string.Join(...) instead.
Under the hood, string.Concat(...) works the same way as string.Join(...). It measures the final string first, allocates it once, and copies the elements into it.
Convert Using Enumerable.Aggregate
The last approach that we are going to show uses Aggregate(...) method:
public string UsingAggregation(string[] array)
{
return array.Aggregate((prev, current) => prev + current);
}
We simply return the result of the Enumerable.Aggregate(...) method.
This method receives a Func delegate as a parameter to apply an accumulator over the array. This Func receives two variables as input parameters, prev and current. The prev represents the accumulator with all the previous elements, while the current represents the next array value.
The Aggregate method efficiency depends on the accumulator function. In our case, we are using the + operator, which creates a new string at every step. However, we could get more performance using the StringBuilder:
public string UsingAggregationWithStringBuilder(string[] array)
{
return array.Aggregate(new StringBuilder(), (prev, current) => prev.Append(current)).ToString();
}
This version passes a StringBuilder as the seed, the starting value of the accumulator.
The seed also matters for empty arrays. Without one, Aggregate starts from the first element, so an empty array throws an InvalidOperationException. With the StringBuilder seed, we get an empty string back, just like we do from the other four approaches.
How Do We Convert a String Array to a String With a Separator?
We use string.Join(...). Its first parameter is the separator, and the method puts it between every two elements, never before the first one or after the last one. With our sample array and a space as the separator, we get Code Maze Code-Maze C#.
The separator can be any string: a space, a comma and a space, a pipe, or Environment.NewLine when we want one element per line. There is also an overload that takes a single char.
The other approaches have no separator parameter. string.Concat(...) glues the elements together as they are. In the two loops, we would have to append the separator ourselves and then deal with the extra one after the last element, or check the index in every iteration, which is a lot more code for the same result.
Null elements don’t break string.Join(...) either. Microsoft Learn says that “if any element in value is null, an empty string is used instead”, so the separators around it stay in place.
Here is the method from our sample:
public string UsingStringJoinWithSeparator(string[] array, string separator)
{
return string.Join(separator, array);
}
When we call it with a space, the elements come out separated, with nothing at either end:
Code Maze Code-Maze C#
The most common separator is a comma, and our article on creating a comma-separated string covers that case for lists and arrays.
Which Way of Converting a String Array Is Fastest?
string.Concat(...) and string.Join(...) share first place. In our runs on .NET 11, they converted 1,000 nine-character strings in a few microseconds, and which of the two comes first changes between runs. Both allocate only the final string, 17.6 KB for 1,000 elements, because they measure it first and allocate it once.
The StringBuilder loop comes close behind. At 1,000 elements, it allocates nearly three times as much memory, because the builder keeps the text in its own buffers and ToString() copies it into a new string at the end. Aggregate(...) with a StringBuilder seed lands in the same group.
The += loop and Aggregate(...) with the + operator are far behind: close to a hundred times slower at 1,000 elements and hundreds of times slower at 10,000. Every step copies the whole string built so far, so ten times more elements cost about a hundred times more memory.
After adding the BenchmarkDotNet NuGet package, we are going to run our benchmark against a small and a big array, with 1,000 and 10,000 elements:
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
[InProcess]
[MemoryDiagnoser]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
public class ArrayConverterBenchmark
{
private readonly ArrayConverter _arrayConverter = new();
private string[] _array = [];
[Params(1_000, 10_000)]
public int Size { get; set; }
[GlobalSetup]
public void Setup()
{
_array = Enumerable.Repeat("Code-Maze", Size).ToArray();
}
[Benchmark]
public string UsingLoopStringAdditionAssignment() => _arrayConverter.UsingLoopStringAdditionAssignment(_array);
[Benchmark]
public string UsingLoopStringBuilder() => _arrayConverter.UsingLoopStringBuilder(_array);
[Benchmark]
public string UsingStringJoin() => _arrayConverter.UsingStringJoin(_array);
[Benchmark]
public string UsingStringConcat() => _arrayConverter.UsingStringConcat(_array);
[Benchmark]
public string UsingAggregation() => _arrayConverter.UsingAggregation(_array);
[Benchmark]
public string UsingAggregationWithStringBuilder() => _arrayConverter.UsingAggregationWithStringBuilder(_array);
}
Every benchmark method returns the string it builds, so the JIT can’t skip the work. [MemoryDiagnoser] adds the Allocated column, which shows how much memory each approach needs. [InProcess] runs the benchmarks inside our application’s own process instead of starting a separate process for them.
Your timings won’t match ours exactly, and they change between runs too. Let’s inspect the results:
| Method | Size | Mean | Error | StdDev | Allocated | |---------------------------------- |------ |---------------:|--------------:|--------------:|-------------:| | UsingStringJoin | 1000 | 5.366 us | 0.1059 us | 0.1617 us | 17.6 KB | | UsingStringConcat | 1000 | 5.445 us | 0.1078 us | 0.2520 us | 17.6 KB | | UsingLoopStringBuilder | 1000 | 7.653 us | 0.1526 us | 0.3251 us | 50 KB | | UsingAggregationWithStringBuilder | 1000 | 7.745 us | 0.1549 us | 0.3885 us | 50 KB | | UsingStringJoin | 10000 | 130.797 us | 3.1700 us | 9.1968 us | 176.02 KB | | UsingStringConcat | 10000 | 131.787 us | 3.1472 us | 9.0804 us | 176.02 KB | | UsingLoopStringBuilder | 10000 | 157.723 us | 3.1285 us | 5.1403 us | 365.37 KB | | UsingAggregationWithStringBuilder | 10000 | 174.775 us | 4.0860 us | 11.9836 us | 365.37 KB | | UsingLoopStringAdditionAssignment | 1000 | 449.679 us | 22.9655 us | 65.8922 us | 8822.23 KB | | UsingAggregation | 1000 | 517.550 us | 16.0606 us | 45.2992 us | 8822.23 KB | | UsingLoopStringAdditionAssignment | 10000 | 116,226.343 us | 2,206.3002 us | 1,842.3606 us | 880086 KB | | UsingAggregation | 10000 | 120,292.181 us | 2,386.4542 us | 3,185.8481 us | 880085.62 KB |
After running this benchmark, we can see that UsingStringConcat and UsingStringJoin are the fastest approaches, and that they are around a hundred times faster than the slowest one (UsingAggregation) when we have an array of 1,000 elements.
This difference is even more significant when we have a larger array. With 10,000 elements, the gap grows to roughly a thousand times, while the array is only ten times bigger.
UsingStringConcat and UsingStringJoin allocate the result once and copy the elements into it, and UsingLoopStringBuilder collects them in a StringBuilder first. On the other hand, UsingLoopStringAdditionAssignment and UsingAggregation concatenate the elements using the + operator, which creates a new, longer string at every step. The benchmark results show that StringBuilder is much more efficient than + when dealing with strings in a loop, and that the built-in methods are better still.
Conclusion
In this article, we have seen five different approaches to converting an array of strings to a string in C#. After inspecting benchmark results, we have seen that the most efficient approaches are the String.Concat and String.Join methods. However, any approach that uses StringBuilder is also very efficient. Now we can choose the approach that best fits our needs.
If we only want to see the elements on the screen, our article on printing an array shows the ways to do that. And when the array holds characters rather than strings, our article on how to convert a char array to a string covers the string constructor and the other options.
Tested with .NET 11.0.0, BenchmarkDotNet 0.15.8 and xUnit 2.9.3.







where is
new string(array);?Hello Cihan Yakar. If you are asking about the full source code, please feel free to visit our linked GitHub repository (at the start of the article), and there you will find all the code from the article and even more. We didn’t add all the code from the project here, just the main parts.