-1
string s = "this is a test";
var result = s.ToCharArray().Reverse();
result.ToString();                               
Console.Write(result.ToArray()); //output = test a si sith  
but with code : Console.WriteLine(result.ToString().ToArray());

and with code:

result.ToString(); 
result.ToArray(); 
Console.Write(result);

The output is :

System.Linq.Enumerable+ReverseIterator`1[System.Char]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • That's what `ToString()` does when it hasn't been overridden; it just shows the name of the runtime type of the object. See duplicate. Your first example works fine because there's [`Write()` method overload](https://learn.microsoft.com/en-us/dotnet/api/system.console.write#System_Console_Write_System_Char___) that accepts the `char[]` returned by `ToArray()`. Note that you're not calling `ToString()` on the thing you're printing in that example, while you are in the second example, and in the third example, the `Write()` method calls `ToString()` implicitly. – Peter Duniho Jun 26 '21 at 04:51

2 Answers2

1

In your case,

var result = s.ToCharArray().Reverse(); //Here type of result is IEnumerable<char>
result.ToString();//Calls default implementation of `.ToString()` method                           

When You call ToString(), it returns resultant string from overridden ToString() function. If it is not implemented in the given class then it returns its base implementation where it returns the fully qualified name of the variable.

In your case, .ToString() function is not overridden in Enumerable class, so it returns system.linq.Enumerable+ReverseIterator1[System.Char] as it is fully qualified name of result variable.

If you want to print elements of result, then try with string.Join(), like

var result = s.ToCharArray().Reverse();
Console.WriteLine(string.Join("", result));  //Instead of result.ToString()

MSDN Documentation: Object.ToString Method

Object.ToString is the major formatting method in the .NET Framework. It converts an object to its string representation so that it is suitable for display. (For information about formatting support in the .NET Framework, see Formatting Types.) Default implementations of the Object.ToString method return the fully qualified name of the object's type.

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
1

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 chars 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.

Sweeper
  • 213,210
  • 22
  • 193
  • 313