C# String Join() Method | Set 1 (original) (raw)

Last Updated : 25 Mar, 2025

In C#, Join() is a string method. This method is used to concatenate the members of a collection or the elements of the specified array, using the specified separator between each member or element. This method can be overloaded by passing different parameters to it.

**Example: Basic use of the Join() method.

C# `

// C# program to join the string using join method using System;

class Geeks { static void Main(string[] args) { // Declaring and initializing the string String []str = {"Geeks", "for", "Geeks"};

    // Using join method to join the string
    System.Console.WriteLine(String.Join(" ", str));

} 

}

`

This method can be overloaded by passing different parameters to it. There are **5 methods in the overloads of the Join() method of which **3 are discussed in this article.

The remaining 2 will be discussed in the Join() method Set 2.

1. String.Join(String, Object [ ])

This method is used to concatenate the elements of an object array with the help of a separator between each element of the object array.

**Syntax:

public static string Join(string separator, params obj[] array)

**Example: Using the Join() method with the array of objects.

C# `

// C# program to demonstrate the // Join(String, Obj [ ]) method using System;

class Geeks { static void Main(string[] args) { // Object of array // Containing different types of values object[] arr = {"Hello", "Geeks", 12345, 786};

    // Using Join method 
    // Here separator is ', '( comma ) 
    string s1 = string.Join(", ", arr); 

    // After performing the Join operation
    Console.WriteLine("Value of string s1 is " + s1); 
} 

}

`

Output

Value of string s1 is Hello, Geeks, 12345, 786

**Explanation: In the above example, we first create an array of objects. Then it is passed to the join method along with the separator which is a ‘, ‘ comma, after the method returns a string as shown in the output.

2. String.Join(String, string [ ])

This method is used to concatenate the elements of a String array with the help of a user-specified separator between each element of the string array.

**Syntax:

public static string Join(string separator, params string[ ] array)

**Example: Using the Join() method with an array of strings.

C# `

// C# program to demonstrate the // Join(String, str[]) method using System;

class Geeks { static void Main(string[] args) { // Array of string
string[] arr = {"Hello", "Geeks", "How", "are", "you"};

    // Using Join method 
    // Here separator is ', '( comma ) 
    string s = string.Join(", ", arr); 

    // Finally after joining process gets over 
    // Getting the output of value of string s 
    Console.WriteLine("Value of string : " + s); 
} 

}

`

Output

Value of string : Hello, Geeks, How, are, you

3. String.Join(String, string [ ], int start, int count)

This method is used to concatenate the elements of a String array between the specified positions with the help of a user-defined separator between each element of the array.

**Syntax:

public static string Join(String separator, params String[] arr, int start, int count)

**Parameters:

**Return Type:

**Exception:

**Example: Using the Join() method to separate the string with the specified count.

C# `

// Use of String.Join( String, String[], Int32, Int32 ) // Method using System;

class Geeks { static void Main(string[] args) { // Creating a string array string[] array = {"Hello", "Geeks", "how", "are" , "you" };

    // Using custom Join method 
    string res = String.Join(", ", array, 0, 2); 

    // Output the value of string s1 
    Console.WriteLine("Value of string res is : " + res); 
} 

}

`

Output

Value of string res is : Hello, Geeks

**Explanation: In the above example, we pass 4 parameters one is the separate and the array and after that the starting index in which we want to start the separation from the string array and the count which defines the number count of string we want to add.