Why does the second one of these produce an exception while the first one doesn't?
string s = null;
MessageBox.Show(s);
MessageBox.Show(s.ToString());
Updated - the exception I can understand, the puzzling bit (to me) is why the first part doesn't show an exception. This isn't anything to do with the Messagebox, as illustrated below.
Eg :
string s = null, msg;
msg = "Message is " + s; //no error
msg = "Message is " + s.ToString(); //error
The first part appears to be implicitly converting a null to a blank string.
because you cannot call instance method ToString()
on a null
reference.
And MessageBox.Show()
is probably implemented to ignore null and print out empty message box.