C# String Concat Method Example

In this source code example, we will demonstrate the usage of String.Concat() method in C# with an example.

The C# Concat() method is used to concatenate multiple string objects. It returns a concatenated string. 

C# String Concat Method Example

using System;

public class StringDemo
{
    public static void Main(string[] args)
    {
        string s1 = "source";
        string s2 = "code";
        string s3 = "examples";
        
        Console.WriteLine(string.Concat(s1,s2));    
        
        Console.WriteLine(string.Concat( string.Concat(s1, s2), s3));    

    }

}

Output:

sourcecode
sourcecodeexamples

C# String Concat(String[]) Method Example

String Concat() method concatenates the elements of a specified String array.

The following example demonstrates the use of the Concat method with a String array.
using System;

public class StringDemo
{
    public static void Main(string[] args)
    {
        // Make an array of strings. Note that we have included spaces.
        string [] s = { "hello ", "and ", "welcome ", "to ",
                        "this ", "demo! " };

        // Put all the strings together.
        Console.WriteLine(string.Concat(s));

        // Sort the strings, and put them together.
        Array.Sort(s);
        
        Console.WriteLine(string.Concat(s));
    }

}

Output:

hello and welcome to this demo! 
and demo! hello this to welcome 

Comments