{"id":191,"date":"2014-05-09T18:45:55","date_gmt":"2014-05-09T17:45:55","guid":{"rendered":"http:\/\/babbacom.com\/?p=191"},"modified":"2016-04-07T23:06:54","modified_gmt":"2016-04-07T22:06:54","slug":"when-is-a-char-not-a-char-when-its-an-int","status":"publish","type":"post","link":"https:\/\/babbacom.com\/?p=191","title":{"rendered":"When is a char not a char? When it&#8217;s an int."},"content":{"rendered":"<p>Just discovered a curious quirk in C#&#8217;s handling of the char type. Most of the time you can just think of it as being a character type, like a single character within a string, but actually it isn&#8217;t. It has an implicit coversion to int, but not to string, so<\/p>\n<pre>Console.WriteLine('A' + 'B');<\/pre>\n<p>would produce 131.<!--more--><\/p>\n<p>I ran into a problem with this because I am currently working on a project that has to read and write lots of ini files. I&#8217;ve written a set of classes for handling ini files, but rather than use the Convert class every time I read a number, and ToString every time I write, I instead created an AutoValue class that automatically carries out all the conversions. This lets me write code like<\/p>\n<pre>string some = ini[\"asection\"][\"some\"];\r\ndecimal other = ini[\"asection\"][\"other\"];\r\nini[\"asection\"][\"other\"] = other + 1;<\/pre>\n<p>It does this by storing the value internally as a string, having a constructor for each type, and implicit conversions in and out of each type. However it went wrong when I tried to do something like<\/p>\n<pre>char c = ini[\"something\"][\"else\"];\r\nbool b = c == 'A';<\/pre>\n<p>because it tried to convert c to an int, even though there is an implicit operator conversion for char. I had to change it to<\/p>\n<pre>bool b = (char)c == 'A';<\/pre>\n<p>to make it use the correct one.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just discovered a curious quirk in C#&#8217;s handling of the char type. Most of the time you can just think of it as being a character type, like a single character within a string, but actually it isn&#8217;t. It has an implicit coversion to int, but not to string, so Console.WriteLine(&#8216;A&#8217; + &#8216;B&#8217;); would produce &hellip; <a href=\"https:\/\/babbacom.com\/?p=191\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;When is a char not a char? When it&#8217;s an int.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[30,31],"class_list":["post-191","post","type-post","status-publish","format-standard","hentry","category-programming","tag-net","tag-c"],"_links":{"self":[{"href":"https:\/\/babbacom.com\/index.php?rest_route=\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/babbacom.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/babbacom.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/babbacom.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/babbacom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=191"}],"version-history":[{"count":3,"href":"https:\/\/babbacom.com\/index.php?rest_route=\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":265,"href":"https:\/\/babbacom.com\/index.php?rest_route=\/wp\/v2\/posts\/191\/revisions\/265"}],"wp:attachment":[{"href":"https:\/\/babbacom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/babbacom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/babbacom.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}