There is a difference because the line:
result.ToString();
does not contribute to what is eventually printed at all. Calling ToString
like that creates the string
"System.Linq.Enumerable+ReverseIterator`1[System.Char]"
And discards it. Note that this does not somehow reassigns that string to result
, as you might think.
So you are essentially comparing:
Console.Write(result.ToArray());
and
Console.WriteLine(result.ToString().ToArray());
Note that result
is an IEnumerable<char>
containing the sequence of characters in the string s
, but reversed. result.ToString().ToArray()
means "call ToString
on result
, then call ToArray
on the return value of ToString
".
Calling ToString
on the IEnumerable<char>
that is result
doesn't give you the characters that are in the sequence, but instead just the type name (this is the default ToString
implementation):
System.Linq.Enumerable+ReverseIterator`1[System.Char]
Then you call ToArray
on that string to convert it to a char[]
, and there is an overload of Console.WriteLine
that prints the characters in a char[]
.
Console.Write(result.ToArray());
is similar, except you don't convert the IEnumerable<char>
to a not-very-useful string, and directly convert the sequence of char
s to a char[]
, so that char[]
actually has the characters of s
in reverse order.
Write
also has an overload that prints the contents of a char[]
, which you then call.