C# String Join Method | Set – 2
Last Updated :
23 Apr, 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:
- Concatenation: The Join() method is used to combine the elements of the collection.
- Separator: It takes a separator by which the element is separated.
- Collections: It can be used with collections like an array, list, or any IEnumerable<string>.
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.
- String.Join(String, IEnumerable<String>)
- String.Join<T>(String, IEnumerable<T>)
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:
- 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#
// 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);
}
}
OutputThe 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:
- 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, 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);
}
}
OutputList 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).