C# String Concat

Summary: in this tutorial, you will learn how to use the C# String Concat() method to concatenate two or more strings into a string.

If you want to concatenate multiple strings with a separator, you can use the Join() method instead.

C# string Concat() method allows you to concatenate multiple strings into one string. The Concat() method has many overloads that provide flexibility for concatenation.

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

This overload concatenates the string representations of four specified read-only character spans into a string. For example:

ReadOnlySpan<char> span1 = "Hello".AsSpan();
ReadOnlySpan<char> span2 = " ".AsSpan();
ReadOnlySpan<char> span3 = "World".AsSpan();
ReadOnlySpan<char> span4 = "!".AsSpan();

var result = string.Concat(span1, span2, span3, span4);
Console.WriteLine(result);Code language: C# (cs)

Output:

Hello World!

Concat(String, String, String, String)

This overload concatenates four specified strings into a string. For example:

string str1 = "Hello";
string str2 = " ";
string str3 = "World";
string str4 = "!";

var result = string.Concat(str1, str2, str3, str4);
Console.WriteLine(result);Code language: C# (cs)

Output:

Hello World!

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

This overload concatenates the string representations of three specified read-only character spans into a string. For example:

ReadOnlySpan<char> span1 = "Hello".AsSpan();
ReadOnlySpan<char> span2 = " ".AsSpan();
ReadOnlySpan<char> span3 = "World".AsSpan();

var result = string.Concat(span1, span2, span3);
Console.WriteLine(result);
Code language: C# (cs)

Output:

Hello World

Concat(Object, Object, Object)

This overload concatenates the string representations of three specified objects into a string. For example:

object obj1 = 42;
object obj2 = " is the answer to ";
object obj3 = "everything";

var result = string.Concat(obj1, obj2, obj3);
Console.WriteLine(result);Code language: C# (cs)

Output:

42 is the answer to everything

Concat(String, String)

This overload concatenates two strings into a string. For example:

string str1 = "Hello";
string str2 = "World";

var result = string.Concat(str1, str2);
Console.WriteLine(result);Code language: C# (cs)

Output:

HelloWorld

Concat(String, String, String)

This overload concatenates three strings into a string. For example:

object obj1 = "Hello";
object obj2 = "World";

var result = string.Concat(obj1, obj2);
Console.WriteLine(result);Code language: C# (cs)

Output:

HelloWorld

Concat(Object, Object)

This overload concatenates the string representations of two specified objects into a string. For example:

object obj1 = "Hello";
object obj2 = "World";

var result = string.Concat(obj1, obj2);
Console.WriteLine(result);Code language: C# (cs)

Output:

HelloWorld

Concat(String[])

This overload concatenates the elements of a specified string array into a string:

string[] strings = { "Hello", " ", "World", "!" };

var result = string.Concat(strings);
Console.WriteLine(result);Code language: C# (cs)

Output:

Hello World!

Concat(Object[])

This overload concatenates the string representations of the elements in a specified object array. For example:

object[] objects = { "Hello", " ", "World", "!" };

var result = string.Concat(objects);
Console.WriteLine(result);Code language: C# (cs)

Output:

Hello World!

Concat(Object)

This overload creates the string representation of a specified object:

object obj = 42;

var result = string.Concat(obj);
Console.WriteLine(result);Code language: C# (cs)

Output:

42

Concat(IEnumerable<String>)

This overload concatenates the members of a constructed IEnumerable<String> collection:

IEnumerable<string> strings = new List<string> { "Hello", " ", "World", "!" };

var result = string.Concat(strings);
Console.WriteLine(result);Code language: C# (cs)

Output:

Hello World!

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

This overload concatenates the string representations of two specified read-only character spans. For example:

ReadOnlySpan<char> span1 = "Hello".AsSpan();
ReadOnlySpan<char> span2 = "World".AsSpan();

var result = string.Concat(span1, span2);
Console.WriteLine(result);
Code language: C# (cs)

Output:

HelloWorld

Concat<T>(IEnumerable<T>)

This overload concatenates the members of an IEnumerable<T> implementation. For example:

IEnumerable<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

var result = string.Concat(numbers);

Console.WriteLine(result);Code language: C# (cs)

Output:

12345

Summary

  • Use C# string Concat() method to concatenate two or more strings into one string.
Was this tutorial helpful ?