Tuesday, June 28, 2011

Convert char[] to String in c#

If you have something like:

char [] chArray = infile.ReadChars(10);

or

char[] chArray = new char[] {'t','e','s','t'};

and you want to convert it to a String, I always (incorrectly) try:

String str = chArray.ToString();

and it doesn't work (you get "System.Char[]").

You have to say something like this:

String str = new String(chArray);

No comments: