C# String Concat Examples

Use string.Concat and the plus operator on strings. Append strings and measure performance.


With concat, strings are merged (combined). It is possible to concatenate 2 or more strings with several syntax forms.


We use the plus operator and the string.Concat method. The plus compiles into string.Concat. Another option is string.Join.

Tip: The main reason to use "+" instead of string.Concat is readability. Both expressions will compile into the same code.

An example.

We concatenate 2 strings with plus. We then use the string.Concat method. The C# compiler converts the plus operator into string.Concat.

However: We concat a string literal ("string1") with a string variable (s1). The reference is stored in the s2 local.

String Literal
C# program that concatenates strings using System; class Program { static void Main() { // ... Create a new string reference. // It points to the literal. string s1 = "string2"; // ... Add another string to the start. string s2 = "string1" + s1; Console.WriteLine(s2); } } Output string1string2

Two strings.

The performance of combining 2 strings with plus or string.Concat is excellent. Using other methods, such as StringBuilder, for 2 strings is slower.

Here: This example shows combines 2 strings with string.Concat. It is the same as using plus.

C# program that uses string.Concat method using System; class Program { static void Main() { string s1 = "string2"; string s2 = string.Concat("string1", s1); Console.WriteLine(s2); } } Output string1string2

Three strings.

Here we use the same string.Concat or plus operator. My testing shows this has good performance and is simpler than other methods such as string.Format.

Note: You can use the + in any order, just like method arguments—which essentially is what the operands become.

C# program that concats 3 strings using System; class Program { static void Main() { string s1 = "string1"; string s2 = "string2"; // Combine 3 strings. string s3 = s1 + s2 + "string3"; // Write the result to the screen. Console.WriteLine(s3); } } Output string1string2string3

Add to start.

Adding to the start is called prepending. You can append strings by using + or string.Concat. But for many appends in a row, consider StringBuilder.StringBuilder

Four strings.

Here we start seeing performance changes. For four strings, it is fastest to add them all in a single statement, with string.Concat or the plus operator.

Tip: The string.Format method also becomes more usable appending many strings here. But it has worse performance.

Also: We start considering StringBuilder when multiple strings are encountered. It has much worse performance at this level.

C# program that concats four strings using System; using System.Text; class Program { static void Main() { string s1 = "string1"; // A. // Concat 4 strings, each in a separate statement. { string s2 = "string1" + s1; s2 += "string2"; s2 += s1; Console.WriteLine(s2); } // B. // Concat 4 strings, in one statement. { 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


For fewer than four strings, using string.Concat or + is fast and also clearest to read. But there are more options for four strings.

Note: To prevent the compiler from solving the program before it is run, I built up one of the strings at runtime.

So: At four strings of short lengths, using string.Concat or the plus operator + on all the strings in one statement is fastest.

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)

Five strings.

We can concat 5 strings. Here the performance radically changes. Looking into the intermediate language, we see a new overload of string.Concat is being used.

Tip: Part A uses two statements to combine the strings. Part B uses a single statement. And part C uses StringBuilder.

Note: I was surprised to find that part A is the fastest. Using string.Concat on all strings at once is suddenly not the best.

C# program that concatenates 5 strings 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

A question.

Why is concatenating 5 strings different? In the .NET Framework, there are Concat methods that accept between 2 and 4 parameters. And there is one that accepts an array.

And: When you concat 5 strings, the array overload method is used. Looking into IL Disassembler, we see a list of many Concat methods.

IL Disassembler

However: There isn't one with 5 arguments. When 5 arguments are needed, the params version is used.

Internal implementation.

The internals use ConcatArray, which seems to have a different implementation. This overload doesn't perform as well as the ones with fewer parameters.Params

Benchmark 2.

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 in a statement.

Note: This won't result in needing the ConcatArray internal method. Understanding exactly how strings are concatenated is extremely useful.

And: The performance difference between four concats at once and five concats at once is relevant.

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)


For fewer than 5 strings, use one statement. This statement should directly use string.Concat or plus. But for 5 or more strings, use multiple statements of 4 strings at once.

Note: This is appropriate for when you have a known number of strings, not an unknown or perhaps large number of strings.

Performance degradation.

If you have a loop or could have many more than 5 strings, use StringBuilder. This may perform worse, but it will prevent edge cases from causing problems.

Also: Microsoft provides information on the string.Concat overloads. Some accept arrays, and some do not.

String.Concat Method: Microsoft Docs


We create a List instance and add 3 string literals to it. We can pass the List variable reference to the string.Concat method. It will concatenate all the strings with no separator.List

Note: The string.Concat method here is equivalent to the string.Join method invocation with an empty separator.

C# program that concats string list 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

Notes, implementation.

Should we use string.Concat or string.Join? The implementations in .NET 4.0 are the same except string.Join repeatedly appends the separator.

Note: Appending an empty string is fast, but not doing so is even faster, so the string.Concat method would be superior here.


Tip: The string.Concat method in the .NET Framework 4.0 has an overload that receives an IEnumerable collection of type string.


A summary.

Strings are frequently combined (concatenated). The benchmarks here give some data data points about what statements are most efficient.

In many programs,

you will append or concat strings. The string.Format method is an alternative. String interpolation can also be used.String Interpolation
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen,