Search code examples
c#.netstringimplicit-conversion

Why aren't values implicitly convertible to string in C#?


I have some code like:

int value = 5;
MessageBox.Show ( value );

and the MessageBox.Show complains saying:

"cannot convert from 'int' to 'string'"

I seem to remember some cases where values seem to be implicitly converted to string values, but can't recall them exactly.

What's the reason behind this decision that any value isn't implicitly convertible to string values?


Solution

  • MessageBox.Show() only accepts a string. When you use something like Debug.WriteLine, it accepts a bunch of different object types, including object, and then calls ToString() on that object. This is probably what you're experiencing.