This is Part 2 in the series of Joining stings

The second way to join strings is to use the String.Concat method.

This method has several signatures that take between two and four strings.

Here is the version that takes two strings:

var son = String.Concat("Bart", "Simpson");

Console.WriteLine(son);

Here is the version that takes three strings:

var daughter = String.Concat("Lisa", "Marie", "Simpson");

Console.WriteLine(daughter);

And here is the version that takes four strings:

var mother = String.Concat("Marjorie", "Jacqueline", "Bouvier", "Simpson");

Console.WriteLine(mother);

Of course the question arises - what if you call String.Concat with more than four strings? Can you?

You can!

var clown = String.Concat("Herschel", "Shmoikel", "Pinchas", "Yerucham", "Krustofsky");

Console.WriteLine(clown);

What happens here is that there is an overload that takes a string array as its parameter.

There is also a version that takes an IEmumerable<string> object as it’s parameter.

This means that you can pass anything that implements IEnumerabe<string> such as a List<string>

var list = new List<string>();

list.Add(son);
list.Add(daughter);
list.Add(mother);
list.Add(clown);

var characters = String.Concat(list);

Of note is that there are overloads that instead of taking two to four strings as parameters, it they take ReadOnlySpan<char> instead.

If you are writing highly performant code using Spans, these overloads would be more appropriate choices.

In the documentation for String.Concat is a remark that touches on what we discovered in the previous post in the series

The code is in my Github

Happy hacking!