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

前端 未结 4 1163
孤城傲影
孤城傲影 2020-12-11 21:09

Im making a hangman game, at the start of the game the word that the player must guess is printed as stars. I have just started making it again after attempting to write it

4条回答
  •  清歌不尽
    2020-12-11 21:58

    Calling ToString on a simple array only returns "T[]" regardless what the type T is. It doesn't 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.

提交回复
热议问题