3

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?

Jesse
  • 1,496
  • 1
  • 9
  • 13
  • See [this question](http://stackoverflow.com/questions/2245581/array-tostring) – jszigeti May 23 '13 at 17:47
  • 3
    ["The default implementation of the ToString method returns the fully qualified name of the type of the Object"](http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx) – Sayse May 23 '13 at 17:48
  • there is a lot of other people who would dislike your idea of array reperesentation. – Felice Pollano May 23 '13 at 17:52
  • Just call the constructor: new string(charArray) –  May 23 '13 at 18:16
  • @FelicePollano, being new to programming I don't understand what you're trying to insinuate. I didn't have an idea about array representation, I just wanted to learn why I couldn't use ToString on an array. – Jesse May 23 '13 at 18:17
  • @Jesse nothing to insinuate: since anyone can claim a different ToString() implementation, the default is left at a basic behavior. – Felice Pollano May 24 '13 at 05:07

2 Answers2

3

While I can't find concrete proof of this (as of yet). I believe you cannot use it on a char array as Array types are reference types meaning they hold references to the actual values rather than the value of each character. It returns "System.Char[]" as stated in my comment; it returns the default value for ToString

EDIT

This was the proof I was looking for https://stackoverflow.com/a/1533770/1324033

Community
  • 1
  • 1
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • thank you for the link 'default value for ToString'.The default implementation of the ToString method returns the fully qualified name of the type of the Object. I see now that many of the C# built-in types have overridden the default behavior of ToString to instead return the value of that particular object instance. Types commonly override the ToString method to return a string that represents the object instance. For example, the base types such as Char, Int32, and String provide ToString implementations that return the string form of the value that the object represents. – Jesse May 23 '13 at 18:13
  • To expand on "I believe you cannot use it on a char array as Array types are reference types," I don't think this is true. After all, string itself is a reference type and you can use ToString on a string. – Jesse May 23 '13 at 18:15
1

You recieve that value becouse that is default value of char[] array and generally object. When you want change .ToString() behaviour for your case, you can override default ToString() method by using (for example) extension method.

Fka
  • 6,044
  • 5
  • 42
  • 60