How to: Concatenate Multiple Strings (C# Guide)

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.


The C# examples in this article run in the Try.NET inline code runner and playground. You can run an example in an interactive window by pressing the Run button. Once you execute the code, you can modify it and run the modified code by pressing Run again. The modified code either runs in the interactive window or, if compilation fails, the interactive window displays all C# compiler error messages.

The following example uses concatenation to split a long string literal into smaller strings in order to improve readability in the source code. These parts are concatenated into a single string at compile time. There is no run-time performance cost regardless of the number of strings involved.

// Concatenation of literals is performed at compile time, not run time.
string text = "Historically, the world of data and the world of objects " +
"have not been well integrated. Programmers work in C# or Visual Basic " +
"and also in SQL or XQuery. On the one side are concepts such as classes, " +
"objects, fields, inheritance, and .NET Framework APIs. On the other side " +
"are tables, columns, rows, nodes, and separate languages for dealing with " +
"them. Data types often require translation between the two worlds; there are " +
"different standard functions. Because the object world has no notion of query, a " +
"query can only be represented as a string without compile-time type checking or " +
"IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to " +
"objects in memory is often tedious and error-prone.";


To concatenate string variables, you can use the + or += operators, string interpolation or the String.Concat, String.Format, String.Join or StringBuilder.Append methods. The + operator is easy to use and makes for intuitive code. Even if you use several + operators in one statement, the string content is copied only once. The following code shows two examples of using the + operator to concatenate strings:

string userName = "<Type your name here>";
string dateString = DateTime.Today.ToShortDateString();

// Use the + and += operators for one-time concatenations.
string str = "Hello " + userName + ". Today is " + dateString + ".";

str += " How are you today?";

In some expressions, it is easier to concatenate strings using string interpolation, as the following code shows:

string userName = "<Type your name here>";
string date = DateTime.Today.ToShortDateString();

// Use string interpolation to concatenate strings.
string str = $"Hello {userName}. Today is {date}.";

str = $"{str} How are you today?";


In string concatenation operations, the C# compiler treats a null string the same as an empty string.

Other method to concatenate strings is String.Format. This method works well when you are building a string from a small number of component strings. This method is also a great choice when you know the number of strings that make up your concatenated string.

In other cases you may be combining strings in a loop, where you don't know how many source strings you are combining, and the actual number of source strings may be quite large. The StringBuilder class was designed for these scenarios. The following code uses the Append method of the StringBuilder class to concatenate strings.

// Use StringBuilder for concatenation in tight loops.
var sb = new System.Text.StringBuilder();
for (int i = 0; i < 20; i++)

You can read more about the reasons to choose string concatenation or the StringBuilder class

Another option to join strings from a collection is to use String.Concat method. Use String.Join method if strings should be separated by a delimeter. The following code combines an array of words using both methods:

string[] words = { "The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog." };

var unreadablePhrase = string.Concat(words);

var readablePhrase = string.Join(" ", words);

At last, you can use LINQ and the Enumerable.Aggregate method to join strings from a collection. This method combines the source strings using a lambda expression. The lambda expression does the work to add each string to the existing accumulation. The following example combines an array of words by adding a space between each word in the array:

string[] words = { "The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog." };

var phrase = words.Aggregate((partialPhrase, word) =>$"{partialPhrase} {word}");

See Also

C# Programming Guide