C# implicit string conversion
Posted by Jonas Elfström Wed, 03 Feb 2010 16:32:00 GMT
I know how it works and I think I can see why but I'm still not very fond of how eager C# is to perform implicit string conversion.
string s = -42 + '+' + "+" + -0.1 / -0.1 + "=" + (7 ^ 5) + " is " + true + " and not " + AddressFamily.Unknown;
s will be set to "1+1=2 is True and not Unknown"
The answer is in white text above, select the text to see it.
A more real problem is something like this
string str = 1 + 2 + "!=" + 1 + 2;
str will be set to "3!=12".
This wouldn't be much of a problem if all objects in .NET always returned a decent string representation of their current state/value with ToString() but that's not the case. Instead "The default implementation returns the fully qualified name of the type of the Object.".
I don't like the inconsistency. It's way too late now but I think it would have been much better if only objects that really produces a human readable output of the data in the object should implement ToString(). If you want the name of the type of the Object there should be another way.