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

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.


A short solution (everywhere you need a string):

 MessageBox.Show(""+value);

But I would prefer a ToString() or a String.Format() in most cases.

To answer the "Why" part: because implicit conversions can be dangerous and can undermine type-safety.
"1" + 2 = "12" = 12, not always what you want or expect.


For the exact reason, you would have to ask either the C# compiler guys, or one of the .NET runtime guys.

However, there are no places in the .NET framework or the C# language where values are automatically and implicitly convertible to strings.

You might, however, think of the way string concatenation works, but that only works because there are a lot of overloads on the string.Concat method, including one that takes an object.

In other words, this is allowed:

string s = "Hello there: " + 4;

Other methods around in the framework also has lots of overloads, such as Debug.WriteLine and such, where it will easily accept your integer or decimal, convert it to a string through a call to .ToString, and then print it.

It isn't, however, something built into string or int, but the method itself.