If you are creating a dynamic string, It is a good practice to opt for
StringBuilder class rather than joining strings using + or
Concat method as each +/
Concat creates a new string object everytime it is executed.
Concat string array elements using String.Join
String.Join method can be used to concatenate multiple elements from a string array.
Produces the following output: "orange, grape"
This example uses the
String.Join(String, String, Int32, Int32) overload, which specifies the start index and count on top of the separator and value.
If you do not wish to use the startIndex and count overloads, you can join all string given. Like this:
which will produce;
apple, orange, grape, pear
Concatenate strings using System.Text.StringBuilder
Concatenating strings using a StringBuilder can offer performance advantages over simple string concatenation using
+. This is due to the way memory is allocated. Strings are reallocated with each concatenation, StringBuilders allocate memory in blocks only reallocating when the current block is exhausted. This can make a huge difference when doing a lot of small concatenations.
Append() can be daisy chained, because it returns a reference to the
Concatenation of two strings using $
$ provides an easy and a concise method to concatenate multiple strings.