C# string.Concat

String type

With concat two or more strings become one. It is possible to concatenate two or more strings with several syntax forms in the C# language. The plus operator and the string.Concat method are used. The plus operator compiles into string.Concat.

This C# tutorial shows how to use string.Concat. It provides many concatenation examples.

Two strings

First, here we see that you can concatenate two strings by using the plus operator or the string.Concat method. Internally, the C# compiler converts the plus operator into string.Concat, so only the syntax is different.

Program that concatenates strings [C#]

using System;

class Program
{
    static void Main()
    {
	// 1.
	// New string called s1.
	string s1 = "string2";

	// 2.
	// Add another string to the start.
	string s2 = "string1" + s1;

	// 3.
	// Write to console.
	Console.WriteLine(s2);
    }
}

Output

string1string2

Note on efficiency. Performance of combining two strings with plus or string.Concat is excellent and using other methods for just two strings is slower. Here's the equivalent code with string.Concat.

Program that uses string.Concat method [C#]

using System;

class Program
{
    static void Main()
    {
	// 1.
	string s1 = "string2";

	// 2.
	string s2 = string.Concat("string1", s1);

	// 3.
	Console.WriteLine(s2);
    }
}

Output

string1string2

Three strings

Note

Here we can use the same string.Concat or plus operator as for two strings. My testing shows this has good performance and is simpler than other methods such as string.Format. string.Concat with three arguments works equally well here too. Note that you can use the + in any order, just like method arguments.

Program that concats three strings [C#]

using System;

class Program
{
    static void Main()
    {
	// 1.
	string s1 = "string1";

	// 2.
	string s2 = "string2";

	// 3.
	// Combine three strings.
	string s3 = s1 + s2 + "string3";

	// 4.
	// Write the result to the screen.
	Console.WriteLine(s3);
    }
}

Output

string1string2string3

Add string to start of another

Programming tip

This is called prepending and you can use the same + or string.Concat methods. Performance here is good and which method to use depends on what you find clearer. You can append strings by using + or string.Concat as well. However, for many appends in a row, consider StringBuilder.

Four strings

To combine four strings, you can use the same syntax above. Here we start seeing performance changes. For four strings, it is fastest to add them all in a single statement, with string.Concat or +. string.Format also becomes more usable appending many strings here. However, it suffers from much worse performance. Additionally, we start considering StringBuilder when multiple strings are encountered. It also has much worse performance at this level.

Program that concats four strings [C#]

using System;
using System.Text;

class Program
{
    static void Main()
    {
	string s1 = "string1";
	// A.
	// Concat 4 strings, one at a time.
	{
	    string s2 = "string1" + s1;
	    s2 += "string2";
	    s2 += s1;
	    Console.WriteLine(s2);
	}
	// B.
	// Concat 4 strings, all at once.
	{
	    string s2 = string.Concat("string1",
		s1,
		"string2",
		s1);
	    Console.WriteLine(s2);
	}
	// C.
	// Concat 4 strings using a format string.
	{
	    string s2 = string.Format("string1{0}string2{0}",
		s1);
	    Console.WriteLine(s2);
	}
	// D.
	// Concat 4 strings, three at a time then one.
	{
	    string s2 = "string1" + s1 + "string2";
	    s2 += s1;
	    Console.WriteLine(s2);
	}
	// E.
	// Concat 4 strings, one at a time with StringBuilder.
	{
	    string s2 = new StringBuilder("string1").Append(
		s1).Append("string2").Append(s1).ToString();
	    Console.WriteLine(s2);
	}
	Console.ReadLine();
    }
}

Output

string1string1string2string1
string1string1string2string1
string1string1string2string1
string1string1string2string1
string1string1string2string1

Overview. When executed, the above console program will show the following output. All five methods resulted in the same exact line output.

Benchmark

Performance optimization

What I display here is the benchmarks for four strings combined into one. For fewer than four strings, using string.Concat or + is fast and also clearest to read. But there are more options for four strings.

I compared the methods A through E above. I had to write several programs to benchmark them, so all the code is not available. To prevent the compiler from solving the program before it is run, I built up one of the strings at runtime. This provides accurate results.

String Concat performance: 4 strings

A:  557 ms (Concat 4 strings, 1 at a time)
B:  281 ms (Concat 4 strings, all at once)
C: 1342 ms (Concat 4 strings with a format string)
D:  421 ms (Concat 4 strings, 3 at a time and then 1)
E:  812 ms (Concat 4 strings, 1 at a time with StringBuilder)

Description. At four strings of short lengths, using string.Concat or the plus operator + on all the strings in one statement is best. The slowest method is string.Format.

Five strings

Here we see that you can concat five strings with the same methods as shown above. Here the performance changes radically and we have to look into the intermediate language to see why.

Program that concatenates five strings [C#]

using System;
using System.Text;

class Program
{
    static void Main()
    {
	string s1 = "string1";
	// A.
	// Concat 5 strings in two statements.
	{
	    string s2 =
		s1 +
		"string1" +
		s1 +
		"string2";
	    s2 += s1;
	    Console.WriteLine(s2);
	}
	// B.
	// Concat 5 strings in one statement.
	{
	    string s2 =
		s1 +
		"string1" +
		s1 +
		"string2" +
		s1;
	    Console.WriteLine(s2);
	}
	// C.
	// Concat 5 strings in StringBuilder.
	{
	    string s2 = new StringBuilder(s1).Append(
		"string1").Append(s1).Append(
		"string2").Append(s1).ToString();
	    Console.WriteLine(s2);
	}
	Console.ReadLine();
    }
}

Output

string1string1string1string2string1
string1string1string1string2string1
string1string1string1string2string1
Letters of the alphabet: ABC

Description. Parts A, B and C above combine five strings in different ways. The first method uses two statements to combine the strings. Part B uses a single statement. Part C uses StringBuilder. I was surprised to find that part A is the fastest here. Suddenly, using string.Concat or + on all strings at once in part B is not the best. Here's some more analysis.

Why is concatenating five strings different? In the .NET Framework, there are string.Concat overloaded methods that accept between two and four parameters. Finally, there is an override that accepts an array. When you concat five strings, the array overload method is used. Looking into IL Disassembler, we see a list of many Concat methods. However, there isn't one with five arguments specifically. When five arguments are needed, the params version is used.

IL Disassembler Tutorial.NET Framework information

Internal implementation. Looking even deeper, I saw that the internals use ConcatArray, which seems to have a much different implementation. Unfortunately, this overload doesn't perform as well as the ones with fewer parameters.

Params Method

Benchmark 2

My research shows that it is fastest to first concat four strings, then concat that with more strings. Instead of combining all strings at once, it is faster to combine four at a time. This won't result in needing the ConcatArray internal method.

String Concat performance: 5 strings

A: 484 ms (Concat 5 strings in two statements)
B: 686 ms (Concat 5 strings in one statement)
C: 889 ms (Concat 5 strings in StringBuilder)
Question and answer

Is this useful? It depends. However, I feel understanding exactly how strings are concatenated is extremely useful. The performance difference between four concats at once and five concats at once is relevant.

MSDN reference

Here we see that MSDN provides information on the string.Concat overloads. Look at the links to the methods that accept arrays and those that don't.

MSDN reference StringBuilder Secrets

String concat guidelines

This section provides information

When you need to combine fewer than five strings, always use one statement. This statement should use string.Concat directly or the + operator. However, When you need to concat five or more strings, use multiple statements of four strings at once. This is appropriate for when you have a known number of strings.

Performance degradation. Further, if you have a loop or could have many more than five strings, use StringBuilder. This may perform worse sometimes, but it will prevent edge cases of huge numbers of strings from causing problems.

Concat string List

In this example program, we create a List instance and add three string literals to it. In the new versions of the .NET Framework, you can pass the List variable reference to the string.Concat method and it will concatenate all the strings in the List together with no separator. As we further demonstrate, the string.Concat method here is equivalent to the string.Join method invocation with an empty separator.

Program that concats string list [C#]

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	// Create list of three strings.
	var list = new List<string>();
	list.Add("cat");
	list.Add("dog");
	list.Add("perls");

	// Concat the list.
	string concat = string.Concat(list);
	Console.WriteLine(concat);

	// Join the list.
	string join = string.Join("", list);
	Console.WriteLine(join);
    }
}

Output

catdogperls
catdogperls

What's the difference? You are probably wondering which of the two versions of the code, string.Concat or string.Join, you should use. As it turns out, the internal implementations in the .NET Framework 4.0 are the same except string.Join appends the separator repeatedly. Appending an empty string is fast, but not doing so is even faster, so the string.Concat method would be superior here.

String Join Method

Tip: The string.Concat method in the .NET Framework 4.0 has an overload that receives an IEnumerable collection of type string; you can use this to quickly convert a string List into a single string with no separators, similar to a string.Join invocation with a zero-length separator specified.

Summary

The C# programming language

We looked into some of the internals of string concatenation in the C# language. We saw samples of combining two, three, four and five strings at once. My benchmarks gave you some data points about what statements are more efficient. In every program, you will likely append or concat strings. The string.Format method is another way to concat strings.

string.Format Method String Type
.NET