A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/c-sharp-join-method-set-2/ below:

C# String Join Method | Set - 2

C# String Join Method | Set - 2

Last Updated : 11 Jul, 2025

The C# Join() method is a method that is present in the String class. This method is used to concatenate or combine different kinds of collections, such as an array, list or set. With the specified separator between each member or element.

Key Features:

Overloads of Join() Method

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

For the first three methods, please refer to the Join() method set-1.

1. 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:

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#
// Use of String.Join(String, IEnumerable<String>) Method
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?
2. 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:

Return Type: This method returns a string of type System.String, user-definedthe 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 T1 is null.

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

C#
// C# program to demonstrate the 
// Join(String, IEnumerable <T > T1) 
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).



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4