I am working through an exercise where I am supposed to ask the user for a string, then reverse the string and print it out to the console. I wrote this:
Console.WriteLine("Please enter a string of characters: ");
char[] words = Console.ReadLine().ToCharArray();
Console.WriteLine("You entered {0}, the reverse of which is {1}", words.ToString(), words.Reverse().ToString());
Which did not work. Apparently sending a char array to .ToString() gives back "System.Char[]" and reversing it gives back "System.Linq.Enumerable+d__a0`1[System.Char]".
I learned that .ToString() cannot handle a char array and instead I had to use the new string constructor, so I rewrote my code to this and it works now:
Console.WriteLine("Please enter a string of characters: ");
char[] words = Console.ReadLine().ToCharArray();
Console.WriteLine("You entered {0}, the reverse of which is {1}", new string(words), new string(words.Reverse().ToArray()));
My question is why can't I use .ToString() on a char array? The summary of ToString vaguely states "Returns a string that represents the current object," which meant to me I would get a string back that represents the char array I am sending it. What am I missing?