In this article, we are going to learn how to convert a string array to a string in C#. We will cover five different approaches to achieve the same result, and in the end, we will inspect benchmark results to see the fastest way to accomplish the conversion. 

To download the source code for this article, you can visit our GitHub repository.

Let’s start.

Convert Using Loop and Addition Assignment Operator

The first and easiest way to convert a string array into a string is using the addition assignment += operator:

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!
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. 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:

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 also uses the StringBuilder class.

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 to use a different approach.

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 Enumeraable.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 each array value.

The Aggregate method efficiency depends on the accumulator function. In our case, we are using += operator. However, we could get more performance using the StringBuilder:

return array.Aggregate(new StringBuilder(), (prev, current) => prev.Append(current)).ToString();

Benchmark Comparison

We are going to run two benchmarks to check our method’s behavior against small and big arrays.

Let’s inspect the result against a small array, running the benchmark with an array of 1,000 elements:

private string[] _array = Enumerable.Repeat("Code-Maze", 1_000).ToArray();

|                           Method |       Mean |      Error |     StdDev |
|--------------------------------- |-----------:|-----------:|-----------:|
|                UsingStringConcat |   7.525 us |  0.0358 us |  0.0299 us |
|                  UsingStringJoin |   9.287 us |  0.1732 us |  0.1447 us |
|           UsingLoopStringBuilder |  10.264 us |  0.2435 us |  0.2606 us |
|                 UsingAggregation | 929.379 us |  6.2232 us |  5.8212 us |
|UsingLoopStringAdditionAssignment | 948.445 us | 17.8103 us | 14.8724 us |

After running this benchmark, we can see that the fastest approach (UsingStringConcat) is more than 126 times faster than the slowest (UsingLoopStringAdditionAssignment) when we have an array of 1,000 elements.

This difference is even more significant when we have a larger array:

|                           Method |          Mean |         Error |        StdDev |
|--------------------------------- |--------------:|--------------:|--------------:|
|                UsingStringConcat |      1.117 ms |     0.0223 ms |     0.0555 ms |
|                  UsingStringJoin |      1.499 ms |     0.0299 ms |     0.0676 ms |
|           UsingLoopStringBuilder |      2.483 ms |     0.0693 ms |     0.2032 ms |
|                 UsingAggregation | 47,873.520 ms | 1,516.6612 ms | 4,471.9098 ms |
|UsingLoopStringAdditionAssignment | 49,610.308 ms | 1,679.1156 ms | 4,950.9103 ms |

As we can see in the benchmark results, when our array has 100,000 elements, the difference between the fastest approach achieves is more than 45,000 ms.

UsingStringConcat, UsingStringJoin, and UsingLoopStringBuilder use the StringBuilder class to concatenate the elements. On the other hand, UsingLoopStringAdditionAssignment and UsingAggregation concatenate the elements using the += operator. The benchmark results show that StringBuilder is much more efficient when dealing with strings.

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 approach is using the String.Concat method. However, any approach that uses StringBuilder is also very efficient. Now we can choose the approach that best fits our needs.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!