Why should we use the ToString method with StringBuilder?

MSDN says we need to convert the StringBuilder object to string , but StringBuilder work fine? Why do we need to convert?

 string[] spellings = { "hi", "hiii", "hiae" }; StringBuilder Builder = new StringBuilder(); int counter = 1; foreach (string value in spellings) { Builder.AppendFormat("({0}) Which is Right spelling? {1}", counter, value); Builder.AppendLine(); counter++; } Console.WriteLine(Builder); // Works Perfectly //Why should i use tostring like below Console.WriteLine(Builder.ToString()); // Does it make any difference in above two ways. Console.ReadLine(); 
+4
source share
4 answers

These two calls use different Console.WriteLine overloads: WriteLine(Object) and WriteLine(String) .

And the WriteLine(Object) overload calls "... the ToString method of the value is called to create its string representation, and the resulting string is written to standard output." ( msdn )

Edit

The only difference I see is:

 StringBuilder sb = null; Console.WriteLine(sb); // prints terminator Console.WriteLine(sb.ToString()); // throws NullReferenceException 
+10
source

In this particular example, both work fine, because Console.WriteLine will call ToString() for you, for everything that you pass it. The following also works:

  Console.WriteLine(3); //called with an int; Console.WriteLine(DateTime.Now); Console.WriteLine(new {}); //called with an object 

However, since StringBuilder not a string (but an object that can make a string for you), you cannot pass it to a method that expects a string, i.e.

 public void PrintMe(string value) { Console.WriteLine(value); } StringBuilder sb = new StringBuilder(); PrintMe(sb); // this will not work 
+2
source

Both lines of code for writing to the console do the same thing differently. Secondly, more obvious.

Console.WriteLine(Builder); uses the overloaded WriteLine() method, which takes an object. As mentioned here, the ToString() method will be called in the Builder :

'If the value is null, only the line terminator is written. Otherwise, the ToString value method is called to create its presentation string, and the resulting string is written to the standard output stream.

In the second line, Console.WriteLine(Builder.ToString()); you call the ToString() method explicitly. This is preferable because the developer can immediately see what the code is doing.

0
source

this is from code in a web form. I added several times to my StringBuilder and accidentally made

 return "[" + sbFinalContents + "]"; 

and I had no problems with that. I accidentally left the ToString () part, and I didn’t even notice for several days. any idea why this is not exploding?

StringBuilder.ToString Method (.net 4.): "You must call the ToString method to convert the StringBuilder object to a String object before passing the string represented by the StringBuilder object to the String method or displaying it in the user interface. "

technically, I don't do a single one (I just return the string), but I'm still surprised that it did not fail.

0
source

Source: https://habr.com/ru/post/1495703/


All Articles