C# `String.Concat()`: Efficient String Concatenation
Master string manipulation in C# with the versatile `String.Concat()` method. This tutorial explores its various overloads, demonstrating how to efficiently combine strings and objects into a single string. Learn different ways to concatenate strings in C#.
Understanding C#'s `String.Concat()` Method
The C# `String.Concat()` method combines multiple strings into a single string. It's a fundamental string manipulation function offering several overloads for flexibility.
`String.Concat()` Method Overloads
The `Concat()` method is overloaded, meaning it has multiple versions with different parameter types. This allows you to concatenate various combinations of strings and objects.
Here are some of the common overloads:
public static string Concat(string str1, string str2)
: Concatenates two strings.public static string Concat(params string[] values)
: Concatenates an array of strings.public static string Concat(params object[] args)
: Concatenates an array of objects (objects are converted to their string representations).public static string Concat(IEnumerable<string> values)
: Concatenates a collection of strings.
Parameters and Return Value
The parameters depend on the overload used, but they generally involve one or more strings or objects. The return value is always a new string that is the result of concatenating all the input strings or string representations of the objects.
Example: Concatenating Strings
string str1 = "Hello";
string str2 = "World";
string combined = string.Concat(str1, " ", str2); // Output: Hello World
Console.WriteLine(combined);