<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Alice, Bob, and Mallory: C# implicit string conversion</title>
    <link>http://www.alicebobandmallory.com/articles/2010/02/03/c-implicit-string-conversion</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>metasyntactics</description>
    <item>
      <title>C# implicit string conversion</title>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Contrived example:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;string s = -&lt;span style="color:#00D;font-weight:bold"&gt;42&lt;/span&gt; + &lt;span style="color:#04D"&gt;'+'&lt;/span&gt; + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;+&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + -&lt;span style="color:#60E;font-weight:bold"&gt;0&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.1&lt;/span&gt; / -&lt;span style="color:#60E;font-weight:bold"&gt;0&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.1&lt;/span&gt; + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;=&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + (&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt; ^ &lt;span style="color:#00D;font-weight:bold"&gt;5&lt;/span&gt;) + &lt;tt&gt;
&lt;/tt&gt;      &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt; is &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + &lt;span style="color:#038;font-weight:bold"&gt;true&lt;/span&gt; + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt; and not &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + AddressFamily.Unknown;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;s will be set to &lt;font color="white"&gt;"1+1=2 is True and not Unknown"&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;The answer is in white text above, select the text to see it.&lt;/p&gt;

&lt;p&gt;A more real problem is something like this&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;string str = &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt; + &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;!=&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt; + &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;code&gt;str&lt;/code&gt; will be set to "3!=12".&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red"&gt;Edit 2010-02-08&lt;/font&gt;&lt;br/&gt;
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. &lt;a href="http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx"&gt;Instead&lt;/a&gt; "The default implementation returns the fully qualified name of the type of the Object.".&lt;br/&gt;
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.&lt;/p&gt;</description>
      <pubDate>Wed, 03 Feb 2010 17:32:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:da6a1f72-b81c-4ee7-b5d1-8f7a821595e3</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2010/02/03/c-implicit-string-conversion</link>
      <category>C#</category>
    </item>
  </channel>
</rss>

