C# .Join()
Published Dec 6, 2024
Contribute to Docs
The .Join() method concatenates an array or a collection of strings into a single string, with a specified separator inserted between each element.
This method is particularly useful for formatting lists, such as comma-separated values or readable sentences.
Syntax
string.Join(string separator, IEnumerable<string> values)
separator: A string to insert between each element. If empty, elements are joined with no separator.values: An array or collection of strings to concatenate.nullvalues in the collection are replaced with empty strings.
Example
This example demonstrates how .Join() uses a comma and space (,) as the separator to create a formatted to-do list.
using System;class Program{static void Main(){string[] studyTasks = { "Complete C# lesson", "Review notes", "Practice coding exercises", "Read documentation" };// Using .Join() to create a sequential to-do list with ", " as the separatorstring toDoList = string.Join(", ", studyTasks);Console.WriteLine("Today's Codecademy study plan: " + toDoList);}}
This example results in the following output:
Today's Codecademy study plan: Complete C# lesson, Review notes, Practice coding exercises, Read documentation
Codebyte Example
The following example demonstrates how .Join() combines a collection of course names using an ampersand (&) to produce a readable list.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C# on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
- Beginner Friendly.15 hours