0
0
CsharpHow-ToBeginner · 3 min read

How to Use string.Concat in C# for String Joining

Use string.Concat in C# to join two or more strings into one without any separator. It takes multiple string arguments or an array of strings and returns their combined result as a single string.
📐

Syntax

The string.Concat method has several overloads. The most common are:

  • string.Concat(string str0, string str1): Joins two strings.
  • string.Concat(params string[] values): Joins multiple strings passed as an array or comma-separated arguments.

It returns a new string that is the combination of all input strings without any separator.

csharp
string result = string.Concat(str0, str1);
string resultMultiple = string.Concat(values);
💻

Example

This example shows how to join two strings and multiple strings using string.Concat. It demonstrates the method joining strings without adding spaces or other characters.

csharp
using System;

class Program
{
    static void Main()
    {
        string first = "Hello";
        string second = "World";

        // Join two strings
        string joinedTwo = string.Concat(first, second);
        Console.WriteLine(joinedTwo); // Output: HelloWorld

        // Join multiple strings
        string joinedMultiple = string.Concat("C#", " ", "is", " ", "fun!");
        Console.WriteLine(joinedMultiple); // Output: C# is fun!
    }
}
Output
HelloWorld C# is fun!
⚠️

Common Pitfalls

Common mistakes when using string.Concat include:

  • Expecting it to add spaces or separators automatically. It simply joins strings as-is.
  • Passing null values which are treated as empty strings, possibly causing unexpected results.
  • Using string.Concat when string.Join is better for adding separators.
csharp
using System;

class Program
{
    static void Main()
    {
        string a = "Hello";
        string b = null;

        // Null is treated as empty string
        string result = string.Concat(a, b, "World");
        Console.WriteLine(result); // Output: HelloWorld

        // Wrong if you want spaces
        string wrong = string.Concat("Hello", "World");
        Console.WriteLine(wrong); // Output: HelloWorld

        // Right way to add spaces
        string right = string.Join(" ", "Hello", "World");
        Console.WriteLine(right); // Output: Hello World
    }
}
Output
HelloWorld HelloWorld Hello World
📊

Quick Reference

string.Concat joins strings without separators. Use string.Join to add separators.

  • string.Concat(str1, str2): Joins two strings.
  • string.Concat(params string[] values): Joins many strings.
  • null values become empty strings.
MethodDescriptionExample
string.Concat(string, string)Joins two strings without separatorstring.Concat("Hi", "There") => "HiThere"
string.Concat(params string[])Joins multiple strings without separatorstring.Concat("A", "B", "C") => "ABC"
Behavior with nullNull treated as empty stringstring.Concat("Hi", null) => "Hi"

Key Takeaways

Use string.Concat to join strings directly without adding spaces or separators.
Pass multiple strings as arguments or an array to string.Concat for joining many strings.
Null values passed to string.Concat are treated as empty strings, not errors.
For joining strings with separators like spaces, prefer string.Join instead of string.Concat.
string.Concat is efficient for simple string joining without formatting.