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
C# program to convert a list of characters into a string
Converting a list of characters into a string is a common operation in C#. There are several approaches to accomplish this task, each suitable for different scenarios and data structures.
Syntax
Following are the main syntax patterns for converting characters to strings −
// Using string constructor with char array
string result = new string(charArray);
// Using string.Join() method
string result = string.Join("", charList);
// Using StringBuilder class
StringBuilder sb = new StringBuilder();
sb.Append(charArray);
string result = sb.ToString();
Using String Constructor with Character Array
The most direct approach is using the string constructor that accepts a character array −
using System;
class Program {
static void Main(string[] args) {
char[] ch = new char[5];
ch[0] = 'H';
ch[1] = 'e';
ch[2] = 'l';
ch[3] = 'l';
ch[4] = 'o';
string myChar = new string(ch);
Console.WriteLine("Converted to string = " + myChar);
}
}
The output of the above code is −
Converted to string = Hello
Using string.Join() Method
When working with a List<char> or other collections, string.Join() provides a flexible solution −
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
List<char> charList = new List<char> {'W', 'o', 'r', 'l', 'd'};
string result = string.Join("", charList);
Console.WriteLine("Converted to string = " + result);
// Alternative: convert to array first
char[] charArray = charList.ToArray();
string result2 = new string(charArray);
Console.WriteLine("Using array conversion = " + result2);
}
}
The output of the above code is −
Converted to string = World Using array conversion = World
Using StringBuilder for Large Collections
For performance with large character collections, StringBuilder is the optimal choice −
using System;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
List<char> charList = new List<char> {'C', '#', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm'};
StringBuilder sb = new StringBuilder();
foreach (char c in charList) {
sb.Append(c);
}
string result = sb.ToString();
Console.WriteLine("Using StringBuilder = " + result);
Console.WriteLine("Length: " + result.Length);
}
}
The output of the above code is −
Using StringBuilder = C# Program Length: 10
Comparison of Methods
| Method | Best For | Performance |
|---|---|---|
| String Constructor | Character arrays | Fastest for arrays |
| string.Join() | Collections, Lists | Good for small to medium collections |
| StringBuilder | Large collections, dynamic building | Best for large datasets |
Conclusion
Converting character collections to strings in C# can be accomplished using string constructors for arrays, string.Join() for collections, or StringBuilder for performance-critical scenarios. Choose the method based on your data structure and performance requirements.
