c# - Why we should use ToString method with StringBuilder? -


msdn says need convert stringbuilder object string, stringbuilder works fine? why should convert?

string[] spellings = { "hi", "hiii", "hiae" };  stringbuilder builder = new stringbuilder();  int counter = 1;  foreach (string value in spellings) {     builder.appendformat("({0}) right spelling? {1}", counter, value);     builder.appendline();     counter++; }  console.writeline(builder); // works //why should use tostring below console.writeline(builder.tostring()); // make difference in above 2 ways.  console.readline(); 

these 2 calls use different console.writeline overloads: writeline(object) , writeline(string).

and writeline(object) overload calls "... tostring method of value called produce string representation, , resulting string written standard output stream." (msdn)

edit

the difference here can see is:

stringbuilder sb = null; console.writeline(sb); // prints terminator console.writeline(sb.tostring()); // throws nullreferenceexception 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -