2

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);

5 Answers 5

4
string[] arrStrings = Array.ConvertAll(arrChars, c => c.ToString());
Sign up to request clarification or add additional context in comments.

1 Comment

This one is nice too, because it's more clear what is happening if you're not familiar with Linq's Select method
4

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();

1 Comment

This is about the most compact way to go, nice one.
2

.NET 2:

char[] arrChars = {'a', 'b', 'c'};
string[] arrStrings = Array.ConvertAll<char, string>(arrChars, delegate(char c)
{
    return c.ToString();
});

Comments

0

using LINQ......

char[] arrChars = {'a', 'b', 'c'};
string[] arrStrings =( from c in arrChars select "" + c).ToArray();

Comments

0
string[] arrStrings = arrChars.Select( c => new String(new []{c}) ).ToArray();

or

string[] arrStrings = arrChars.Select( c => c.ToString() ).ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.