Alice, Bob, and Mallory: C# implicit string conversionmetasyntacticstag:alicebobandmallory.com,2005:TypoTypo2010-02-08T01:03:48+01:00Jonas Elfströmurn:uuid:da6a1f72-b81c-4ee7-b5d1-8f7a821595e32010-02-03T17:32:00+01:002010-02-08T01:03:48+01:00C# implicit string conversion<p>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.</p>
<p>Contrived example:</p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
</pre></td>
<td class="code"><pre>string s = -<span style="color:#00D">42</span> + <span style="color:#D20">'+'</span> + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">+</span><span style="color:#710">"</span></span> + -<span style="color:#60E">0</span><span style="color:#60E">.1</span> / -<span style="color:#60E">0</span><span style="color:#60E">.1</span> + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">=</span><span style="color:#710">"</span></span> + (<span style="color:#00D">7</span> ^ <span style="color:#00D">5</span>) +
<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20"> is </span><span style="color:#710">"</span></span> + <span style="color:#069">true</span> + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20"> and not </span><span style="color:#710">"</span></span> + AddressFamily.Unknown;</pre></td>
</tr></table>
<p>s will be set to <font color="white">"1+1=2 is True and not Unknown"</font></p>
<p>The answer is in white text above, select the text to see it.</p>
<p>A more real problem is something like this</p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>
</pre></td>
<td class="code"><pre>string str = <span style="color:#00D">1</span> + <span style="color:#00D">2</span> + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">!=</span><span style="color:#710">"</span></span> + <span style="color:#00D">1</span> + <span style="color:#00D">2</span>;</pre></td>
</tr></table>
<p><code>str</code> will be set to "3!=12".</p>
<p><font style="color:red">Edit 2010-02-08</font><br/>
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. <a href="http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx">Instead</a> "The default implementation returns the fully qualified name of the type of the Object.".<br/>
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.</p><p>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.</p>
<p>Contrived example:</p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
</pre></td>
<td class="code"><pre>string s = -<span style="color:#00D">42</span> + <span style="color:#D20">'+'</span> + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">+</span><span style="color:#710">"</span></span> + -<span style="color:#60E">0</span><span style="color:#60E">.1</span> / -<span style="color:#60E">0</span><span style="color:#60E">.1</span> + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">=</span><span style="color:#710">"</span></span> + (<span style="color:#00D">7</span> ^ <span style="color:#00D">5</span>) +
<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20"> is </span><span style="color:#710">"</span></span> + <span style="color:#069">true</span> + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20"> and not </span><span style="color:#710">"</span></span> + AddressFamily.Unknown;</pre></td>
</tr></table>
<p>s will be set to <font color="white">"1+1=2 is True and not Unknown"</font></p>
<p>The answer is in white text above, select the text to see it.</p>
<p>A more real problem is something like this</p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>
</pre></td>
<td class="code"><pre>string str = <span style="color:#00D">1</span> + <span style="color:#00D">2</span> + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">!=</span><span style="color:#710">"</span></span> + <span style="color:#00D">1</span> + <span style="color:#00D">2</span>;</pre></td>
</tr></table>
<p><code>str</code> will be set to "3!=12".</p>
<p><font style="color:red">Edit 2010-02-08</font><br/>
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. <a href="http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx">Instead</a> "The default implementation returns the fully qualified name of the type of the Object.".<br/>
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.</p>