The string class constructor takes an array of characters to create a new string from an array of characters. In this article, learn how to convert a char array to a string in C#.
The following code creates two strings, first from a string and second by direct passing the array in the constructor.
// Convert char array to string
char[] chars = new char[10];
chars[0] = 'M';
chars[1] = 'a';
chars[2] = 'h';
chars[3] = 'e';
chars[4] = 's';
chars[5] = 'h';
string charsStr = new string(chars);
string charsStr2 = new string(new char[]
{'T','h','i','s',' ','i','s',' ','a',' ','s','t','r','i','n','g'});
Here is a complete sample code:
public void StringToCharArray()
{
// Convert string to char array
string sentence = "Mahesh Chand";
char[] charArr = sentence.ToCharArray();
foreach (char ch in charArr)
{
Console.WriteLine(ch);
}
// Convert char array to string
char[] chars = new char[10];
chars[0] = 'M';
chars[1] = 'a';
chars[2] = 'h';
chars[3] = 'e';
chars[4] = 's';
chars[5] = 'h';
string charsStr = new string(chars);
string charsStr2 = new string(new char[]
{'T','h','i','s',' ','i','s',' ','a',' ','s','t','r','i','n','g'});
Console.WriteLine("Chars to string: {0}", charsStr);
Console.WriteLine("Chars to string: {0}", charsStr2);
}
The output looks like the following:


Bohdan StupakPosted Jun 20, 2022, 3:15 PM
For some reason I was under the impression that there is an implicit conversion between the array of chars and string. When I discovered that there is none this article helped me.
binod kmPosted Oct 23, 2012, 6:55 AM
hey thanks man for the article. but do the how will you do the same if the array is a multidimentional one. say i need to convert all rows of a 2D array arr[5,5] one after another into a string for some manipulation, how will i do that. thanks again