I am following online tutorials re-learning Visual Basic as incarnated in Visual Studio 2015. The tutorials are very boring, but I'm going through them anyway in case a gem or two might be hiding in them. In one tutorial, we create a String, convert it to a CharArray, reverse the array, then print it out to the console one character at a time. All good. Then I decided to just convert the array back to a String and use WriteLine, for kicks...
Module Module1
Sub Main()
Dim myText As String = "Hey, this is a string!"
Console.WriteLine(myText)
Dim myCharArray() As Char = myText.ToCharArray()
Array.Reverse(myCharArray)
Dim reversedText As String = myCharArray.ToString()
Console.WriteLine(reversedText)
' Console output is:
' Hey, this is a string!
' System.char[]
' Even less intuitive, for me, this:
Console.WriteLine(myCharArray)
' produces this:
' !gnirts a si siht ,yeH
' in the console. While this:
Console.WriteLine(myCharArray.ToString())
' gives System.char[] again.
Console.ReadLine()
End Sub
End Module
Obviously, WriteLine has no issue with writing out an array of characters. My question is why it prints the String conversion of a character array as System.char[], even when that conversion is explicitly assigned to an object of type String, when it has no problem printing other String objects as their actual string representation.
I realize this is a trivial and stupid thing to ask about, but if there is an underlying cause that knowledge of will help me avoid a bug or two, I would like to know it.
ToStringconverts theChar[]to a string, string representation of type name. But you need to convert it to the string created from characters in the array.