
{"id":932,"date":"2012-03-12T16:33:54","date_gmt":"2012-03-12T20:33:54","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=932"},"modified":"2012-03-12T16:33:54","modified_gmt":"2012-03-12T20:33:54","slug":"c-namespaces-and-backward-compatibility-part-1","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=932","title":{"rendered":"C# Namespaces and Backward Compatibility &#8211; Part 1"},"content":{"rendered":"<p>Namespaces are a great way to organize code. However, I recently found out that even putting your classes in a carefully organized namespace hierarchy will not shield you from name conflict problems as you move to newer versions of .NET framework or third party libraries.<\/p>\n<p>I ran into two rather unfortunate scenarios in the last couple of days. This post describes the first scenario (hence, &#8220;part 1&#8221;).<\/p>\n<p><b>It was your class, but now it&#8217;s their class.<\/b><\/p>\n<p>Long time ago, I created class <code>Lazy&lt;T&gt;<\/code> that implemented lazy initialization for objects of type <code>T<\/code>. One would use it like this:<\/p>\n<p><code><\/p>\n<pre>using System;\nusing Ikriv.Util;\n\nclass Foo\n{\n    Lazy&lt;DateTime&gt; _someDate\n\n    public Foo() { _someDate = new Lazy<dateTime>(CalculateDate); }\n    private DateTime CalculateDate() { <font color=\"darkgreen\">\/\/ some long calculation here<\/font> }\n}<\/pre>\n<p><\/code><\/p>\n<p>This code worked perfectly under .NET 3.5 and earlier. But then, on a lucky day, Microsoft decided to implement their own <code>Lazy&lt;T&gt;<\/code> in .NET Framework 4, and put it in the <code>System<\/code> namespace. So, when I compile the above code under .NET 4, I get an error, saying that there is an ambiguity between <code>System.Lazy<\/code> and <code>Ikriv.Util.Lazy<\/code>.<\/p>\n<p>If <code>Lazy<\/code> were not a template, I could have resolved it by specifying<\/p>\n<p><code>using Lazy = Ikriv.Util.Lazy;<\/code><\/p>\n<p>Unfortunately, this does not work with templates. So, now I have to choose between these options:<\/p>\n<ul>\n<li>Fully qualify <code>Ikriv.Util.Lazy<\/code> every time it is used. Code readability will suffer.<\/li>\n<li>Rename <code>Ikriv.Util.Lazy<\/code> to something else. This will change previously published interface, which is not good. Also, I can&#8217;t think of a better name: the very reason I had the name conflict is because Microsoft chose the same name as I did, probably because this is the most natural name for this class.<\/li>\n<li>Convert the code to use <code>System.Lazy<\/code> instead of <code>Ikriv.Util.Lazy<\/code>. This will take time and some testing. Fortunately, the two classes are virtually identical, but we may not be that lucky next time.<\/li>\n<\/ul>\n<p>Frankly, I don&#8217;t like any of those solutions.<\/p>\n<p>How could I have avoided the problem? I am not sure. After all, I cannot anticipate what classes Microsoft will decide to throw into the <code>System<\/code> namespace in the future. I could probably prefix all my classes with something like <code>Ivk<\/code> (<code>Ikriv.Util.IvkLazy<\/code>?), but then what&#8217;s the point of namespaces? Also, <code>Ivk<\/code> peppered everywhere will affect code readability.<\/p>\n<p>The good news is that we don&#8217;t upgrade to new version of .NET framework every day, so events of this kind should be rare.<\/p>\n<p><a style=\"display:none\" href=\"http:\/\/www.codeproject.com\/script\/Articles\/BlogFeedList.aspx?amid=1181663\" rel=\"tag\">CodeProject<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Namespaces are a great way to organize code. However, I recently found out that even putting your classes in a carefully organized namespace hierarchy will not shield you from name <a href=\"https:\/\/ikriv.com\/blog\/?p=932\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[8,5],"tags":[],"class_list":["entry","author-ikriv","post-932","post","type-post","status-publish","format-standard","category-cs","category-dev"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=932"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/932\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}