Sumeet Singh Sumeet Singh - 2 months ago 11
C# Question

Why explicitly call ToString() method in MessageBox.show()?

I gather that

MessageBox.show()
method accepts string argument and therefore we require
ToString()
method to call on the argument.

object k = 999;
MessageBox.Show(k.ToString());


But when it comes to
Console.WriteLine()
, it is smart enough to do this by itself. My question is Why the difference?

Answer

If you have a look at http://referencesource.microsoft.com/#mscorlib/system/console.cs,8c83114cc3fd109d

then you'll see that there are many overloads of the WriteLine function in the Console class, including Console.WriteLine(Object value). This is very convenient.

MessageBox.Show lacks these different overloads and excepts "only" a string.

Comments