Open In App

String Join() Method in C#

Last Updated : 10 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

  • Concatenation: This method is used to combine multiple strings into a single string.
  • Separator: This method uses a separator to separate the string.
  • Collections: It can be used with collections like an array, list or any IEnumerable<string>.

Example: Basic use of the Join() method.

C#
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));
	} 
} 

Output
Geeks for Geeks

This method can be overloaded by passing different parameters to it.

  • String.Join(String, Object [ ])
  • String.Join(String, string [ ])
  • String.Join(String, string [ ], int startIndex, int count)
  • String.Join(String, IEnumerable<String>)
  • String.Join<T>(String, IEnumerable<T>)

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)

  • Parameters: separator (string, used as delimiter), values (object[], elements to concatenate).
  • Return Type: It returns a string which is a type of System.String.
  • Exception: This method can give ArgumentNullException if the array is null.

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

C#
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)

  • Parameters: separator (string, defines delimiter), values (string[], elements to concatenate).
  • Return Type: The return type of this method is System.String.
  • Exception: This method can give ArgumentNullException if the array is null.

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

C#
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: 

  • separator: It is a type of System.String which is used to define a separator according to the user’s choice.
  • String [] arr: It is an array of type System.String[] which contains the elements to be concatenated.
  • Start: index in the array (value) at which to start joining the elements.
  • count: Defines how many elements (from startIndex onward) will be included in the resulting string.

Return Type:

  • It returns a string that contains the selected elements of the array, which is separated by the specified separator.

Exception:

  • ArgumentOutOfRangeException: if the start and count are less than zero or greater than the number of elements in the array
  • ArgumentNullException: If the array contains a null value.

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

C#
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.

4. String.Join(String, IEnumerable<String>)

This method is used to concatenate the members of a constructed collection of type String, using the specified separator between each member.

Syntax:

public static string Join(string separator, IEnumerable L1)

Parameters:

  • separator: It is a string which is used as a separator. separator is included in the returned string only if values has more than one element and type of this is System.String.
  • L1: It is a collection that contains the strings to concatenate and type of this is System.Collections.Generic.IEnumerable<String>.

Return Type: This method returns a string of type System.String, which consists of the members of values delimited by the separator string. If values has no members, the method returns String.Empty.

Exception: This method can give ArgumentNullException if the L1 is null.

Example: Joining the elements of a list of type String with the separator hyphen(-) using String.Join() method.

C#
using System; 
using System.Collections.Generic;

class Geeks 
{ 
    static void Main(string[] args) 
    { 
        // creating a list of string using the List class form collections
        List<String> alpha = new List<string>()
		{"Hello", "Geeks", "How", "are", "you?"}; 
  
        // passing the object of list type along with the separator 
        string res = string.Join("-", alpha); 
  
        // getting the value of the string..
        Console.WriteLine("The result value is res: " + res); 
    }
} 

Output
The result value is res: Hello-Geeks-How-are-you?

5. String.Join<T>(String, IEnumerable<T>)

This method is a generic<T> overload of the String.Join() method, which is used to concatenate elements of any generic collection (that implements IEnumerable<T>) into a single string, with a specified separator between each element.

Syntax:

public static string Join(string separator, IEnumerable T1)

Parameters:

  • separator: It is a string which is used as a separator. separator is included in the returned string only if values has more than one element and type of this is System.String.
  • T1: It is a collection that contains the objects to concatenate and type of this is System.Collections.Generic.IEnumerable<T>.

Return Type: This method returns a string of type System.String, which consists of the members of the collection delimited by the specified separator. If the collection has no elements, the method returns String.Empty.

Exception: This method can give ArgumentNullException, if the T1 is null.

Example: Using the Join() method to separate the list of user-defined data type with the seperator hyphen (-).

C#
using System; 
using System.Collections.Generic; 
	
// making a user defined data type
public class Books { 	
	public string book; 
	
	// constructor to hold the string values of item class 
	public Books(string book){ 
		this.book = book; 
	} 

	public override string ToString(){ 
		return this.book; 
	} 
} 

class Geeks{ 
	static void Main(string[] args){ 
		// adding the objects of item class into a list of item class
		var list = new List<Books>(){
			new Books("C#"),
			new Books("Java"),
			new Books("Kotlin"),
			new Books("Javascript")
		}; 

		// passing the list of objects of item class to join method() 
		string res = string.Join("-", list); 
		
        System.Console.WriteLine("List of Books:");
		Console.WriteLine("The values res: " + res); 

	} 
} 

Output
List of Books:
The values res: C#-Java-Kotlin-Javascript

Explanation: In the above example, we create a class named Books and store the data of user-defined type (Book) and store it in the list and then separate it into a single string using the Join() method with the separator - (hyphen).


Article Tags :

Explore