Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I initialize a new string array from the values contained in a char array?

Tags:

c#

I would like to initialize a new string array from the values contained in a char array.
Is this possible without using a list?

This is what I have so far:

char[] arrChars = {'a', 'b', 'c'};
string[] arrStrings = new string[](arrChars);
like image 870
Karim Avatar asked Nov 21 '25 02:11

Karim


2 Answers

string[] arrStrings = Array.ConvertAll(arrChars, c => c.ToString());
like image 64
LukeH Avatar answered Nov 22 '25 15:11

LukeH


Why not use a for loop for your initialization? Or, if that's too many LOC, you could just use Linq:

string[] arrStrings = arrChars.Select(c => c.ToString()).ToArray();
like image 36
Michael Petito Avatar answered Nov 22 '25 17:11

Michael Petito