Array.ToString() returning System.Char[] c#

Array.ToString() returning System.Char[] c#

Calling ToString on a simple array only returns T[] regardless what the type T is. It doesnt have any special handling for char[].

To convert a char[] to string you can use:

string s = new string(charArray);

But for your concrete problem there is an even simpler solution:

string stars = new string(*, PlayerOneWord.Length);

The constructor public String(char c, int count) repeats c count times.

The variable stars is an array of chars. This is the reason you get this error. As it is stated in MSDN

Returns a string that represents the current object.

In order you get a string from the characters in this array, you could use this:

 Console.Write(Word to Guess: {0} , new String(stars));

Array.ToString() returning System.Char[] c#

The correct way to do this would be:

string StarString = new string(stars);

ToString() calls the standard implementation of the Array-classs ToString-method which is the same for all Arrays and similarily to object only returns the fully qualified class name.

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *