C#

String Concatenation

A common use of string concatenation (kon-kat-en-ay-shun) is to inject variable values into the string using the string formatting syntax. C# provides the string operator + (plus symbol) or the String.Format method to format and organize various strings. Both ways can be used to join (concatenate) two or more strings together to generate a new string. You can also use the assignment operator with the plus sign +=, which appends a string to another and creates a new string.

Note:

  • Strings are immutable.
  • The result of string concatenation with any other type is always a string.
  • If an operand of string concatenation is null, an empty string is substituted.

String.Concat( ) and “+”

The variable result in the following example would contain the value a and b after concatenating all the strings.

int a = 2;
int b = 5;

//+ operator concatenates two strings
string result = "Addition of " + a + " + " + b + " is " + (a + b);

Here, a and b are numeric. Operands that are not of type string will be automatically converted to its string representation by calling the virtual ToString method on that operand.

//+ symbol actually translates to the String.Concat() method
string result = String.Concat("Addition of ", a, " + ", b, " is ", (a + b));

OR

string result = string.Concat(new object[] {"Addition of ", a, " + ", b, " is ", (a + b)});

The C# compiler converts the + operator into the static String.Concat() method at compile time; there is no real benefit in choosing one method over the other.

OUTPUT

Addition of 2 + 5 is 7

Frequent use of the concatenation methods above should be avoided because it causes overhead; every time the + operator is used, a new string is created in memory, and the old string is discarded.

Note: To repeatedly build up a string with the + operator is inefficient, and a better approach is to use the System.Text.StringBuilder type.

String.Join( )

The String.Join method takes individual elements from an array and concatenates them with a separator to output a well-formatted string.

string result = String.Join(String.Empty, new object[] {"Addition of ", a, " + ", b, " is ", (a + b)});

OR

string result1 = String.Join(String.Empty, "Addition of ", a, " + ", b, " is ", (a + b));

String.Format( )

The String class also provides a String.Format method which formats values according to position and uses index list as an argument. In the example below, the first parameter of the String.Format method that is used accepts a format string with placeholders (parameter specifiers) indicated by {0}, {1} and {2} that identifies a corresponding item in the index list. These placeholder will be replaced by the values of a and b variables in the formatted string.

String Concatenation

String Interpolation (New Way)

Let’s take a look at one of the handy features in C# named string interpolation which provides another option on how to insert variable values into a string. C# 6.0 introduced interpolated strings through a new Roslyn compiler in Visual Studio 2015 and .NET 4.6 just to accelerate your coding skill. Now you can insert one or more expressions directly in the string literal just by adding the $ prefix to output the result in a nicely formatted manner. When you use interpolated strings, the compiler translates them to String.Format methods under the hood. As a result, interpolated strings are less efficient than directly using the format method. However, interpolated strings are more readable and less error-prone than any other previous methods of formatting strings.

Syntax

The following example uses string interpolation to create the string result using the $ prefix. A string with a $ sign can use curly braces {…} around the name of a variable to output the value assigned to it at that position in the string. Without the leading $ symbol, the string {…} would be treated as a string literal.

int a = 2;
int b = 5;

string result = $"Addition of {a} + {b} is {a + b}"; // string interpolation

which translates to

string result = String.Format("Addition of {0} + {1} is {2}", a, b, (a+b));
OUTPUT

Addition of 2 + 5 is 7

You can use any valid C# expression within the braces. Let’s change the formatting by appending the expression with a colon (:) and a currency formatter to represent a currency amount.

decimal saving = 786.78m;
Console.WriteLine($"My saving this month is {saving:C}.");

Run Demo

OUTPUT

My saving this month is $786.78.

Terms Used

Immutable
A type of object whose data cannot be changed after its creation.

C# Reference | Microsoft Docs