EN
C# / .NET - convert List to string
0 points
In this article, we would like to show you how to convert List to String in C#.
Quick solution:
xxxxxxxxxx
1
List<string> myList = new List<string> { "A", "B", "C" };
2
3
string result = string.Join("", myList);
4
5
Console.WriteLine(result); // Output: "ABC"
In this example, we join myList
items using string.Join()
method with no separator (empty string as separator ""
).
xxxxxxxxxx
1
using System;
2
using System.Collections.Generic;
3
4
public class TestClass
5
{
6
public static void Main()
7
{
8
List<string> myList = new List<string> { "A", "B", "C" };
9
10
string result = string.Join("", myList);
11
12
Console.WriteLine(result);
13
}
14
}
Output:
xxxxxxxxxx
1
ABC
In this example, we join items from myList using string.Join()
method with comma (","
) as a separator.
xxxxxxxxxx
1
using System;
2
using System.Collections.Generic;
3
4
public class TestClass
5
{
6
public static void Main()
7
{
8
List<int> myList = new List<int> { 1, 2, 3 };
9
10
string result = string.Join(",", myList);
11
12
Console.WriteLine(result);
13
}
14
}
Output:
xxxxxxxxxx
1
1,2,3