Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert a list to string in C#?
In C#, you can convert a List to a string using various methods. The most common and efficient approach is using the string.Join() method, which concatenates all elements of a list into a single string with a specified delimiter.
Syntax
Following is the syntax for converting a list to string using string.Join() −
string result = string.Join(delimiter, list);
Where delimiter is the separator between elements, and list is your List<string> collection.
Using string.Join() Method
The string.Join() method is the most efficient way to convert a list to string. It accepts a delimiter and the list as parameters −
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
List<string> l = new List<string>();
// Add elements to the list
l.Add("Accessories");
l.Add("Footwear");
l.Add("Watches");
// Convert list to string with space delimiter
string str = string.Join(" ", l);
Console.WriteLine("With space delimiter: " + str);
// Convert with comma delimiter
string strComma = string.Join(", ", l);
Console.WriteLine("With comma delimiter: " + strComma);
}
}
The output of the above code is −
With space delimiter: Accessories Footwear Watches With comma delimiter: Accessories, Footwear, Watches
Using StringBuilder for Large Lists
For very large lists where performance is critical, you can use StringBuilder −
using System;
using System.Collections.Generic;
using System.Text;
class Demo {
static void Main() {
List<string> colors = new List<string> {"Red", "Blue", "Green", "Yellow"};
StringBuilder sb = new StringBuilder();
for (int i = 0; i < colors.Count; i++) {
sb.Append(colors[i]);
if (i < colors.Count - 1) {
sb.Append(" | ");
}
}
string result = sb.ToString();
Console.WriteLine("Using StringBuilder: " + result);
}
}
The output of the above code is −
Using StringBuilder: Red | Blue | Green | Yellow
Converting List<int> to String
You can also convert a list of integers to string using the same approach −
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
List<int> numbers = new List<int> {1, 2, 3, 4, 5};
// Convert int list to string
string result = string.Join("-", numbers.Select(x => x.ToString()));
Console.WriteLine("Numbers as string: " + result);
// Alternative approach
string result2 = string.Join(", ", numbers);
Console.WriteLine("Alternative approach: " + result2);
}
}
The output of the above code is −
Numbers as string: 1-2-3-4-5 Alternative approach: 1, 2, 3, 4, 5
Common Use Cases
| Method | Best For | Performance |
|---|---|---|
| string.Join() | Most scenarios, clean and readable | Excellent |
| StringBuilder | Very large lists or complex formatting | Good for large data |
| LINQ Select + Join | When transformation is needed | Good for moderate data |
Conclusion
Converting a List to string in C# is most efficiently done using string.Join() method. This approach is clean, readable, and performs well for most scenarios. For specialized cases involving large datasets or complex formatting, consider using StringBuilder or LINQ methods.
