We have many methods in C# to convert a list of chars to a single string in C#, the methods are listed below,
- Using
String.Concat()
Method - Using
String.Join()
Method - Using
String
Class Constructor andToArray(
) Method
Using String.Concat()
Method
The String.Concat()
is used to concatenate the given list of characters separated by an empty string as shown below,
using System;
using System.Collections.Generic;
namespace ConvertCharToString
{
class Program
{
static void Main()
{
List<char> chars = new List<char> { 'X', 'Y', 'Z' };
string str = string.Concat(chars);
Console.WriteLine(str);
Console.ReadKey();
}
}
}
Output:
XYZ
Using String.Join()
Method
The String.Join
method is also used to append characters of a list to a string separated by a given delimiter. In the below example, we use the String.Join()
method to convert the list of chars to a single string.
using System;
using System.Collections.Generic;
namespace ConvertCharToString
{
class Program
{
static void Main()
{
List<char> chars = new List<char> { 'X', 'Y', 'Z' };
string str = string.Join("", chars);
Console.WriteLine(str);
Console.ReadKey();
}
}
}
Output:
XYZ
Using String
Class Constructor and ToArray()
Method
Alternatively, we use String
class Constructor and ToArray()
Method to convert the list of chars to a single string in C# as shown below,
using System;
using System.Collections.Generic;
namespace ConvertCharToString
{
class Program
{
static void Main()
{
List<char> chars = new List<char> { 'X', 'Y', 'Z' };
string str = new string(chars.ToArray());
Console.WriteLine(str);
Console.ReadKey();
}
}
}
Output:
XYZ
Comments (0)