{"id":1053,"date":"2016-05-28T12:01:26","date_gmt":"2016-05-28T16:01:26","guid":{"rendered":"http:\/\/www.peteonsoftware.com\/?p=1053"},"modified":"2016-05-28T12:01:50","modified_gmt":"2016-05-28T16:01:50","slug":"c-6-features-using-static","status":"publish","type":"post","link":"https:\/\/www.peteonsoftware.com\/index.php\/2016\/05\/28\/c-6-features-using-static\/","title":{"rendered":"C# 6 Features &#8211; Using Static"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201605\/Static.jpg\" style=\"float:left; margin: .5em;\" alt=\"Static\" title=\"Static\" \/>For this next post in <a href=\"https:\/\/www.peteonsoftware.com\/index.php\/category\/c-6\/\">my series on the new features in C# 6<\/a>, I&#8217;m going to cover the <em>using static<\/em> syntax.  This feature is also 100% syntactic sugar, but it can be helpful.  Unfortunately, it also comes with quite a few gotchas.<\/p>\n<p>I&#8217;m sure all of us are familiar with writing code like you see below.  The WriteLine() method is a static method on the static class Console.<\/p>\n<pre class=\"code:csharp\">\r\nusing System;\r\n\r\nnamespace CodeSandbox\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            Console.WriteLine(\"Hello World\");\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Because Console is a static class, we can treat it as a &#8220;given&#8221; and just now include it in our using statements.  Console.WriteLine() is actually System.Console.WriteLine() if we include the namespace.  However, since we are including &#8220;using System;&#8221; at the top of the file, we don&#8217;t have to type it out.  This is the exact same concept.<\/p>\n<pre class=\"code:csharp\">\r\nusing static System.Console;\r\n\r\nnamespace CodeSandbox\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            WriteLine(\"Hello World\");\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Now instead of just &#8220;using&#8221;, I&#8217;ve included &#8220;using static&#8221; and included the full name of the static class, in this case System.Console.  Once that is done, I&#8217;m free to just call the WriteLine() method as if it were locally scoped inside my class.  This is all just really syntatic sugar.  When I use Telerik&#8217;s JustDecompile to analyze the program and give me back the source, this is what I get.<\/p>\n<pre class=\"code:csharp\">\r\nusing System;\r\n\r\nnamespace CodeSandbox\r\n{\r\n    internal class Program\r\n    {\r\n        public Program()\r\n        {\r\n        }\r\n\r\n        private static void Main(string[] args)\r\n        {\r\n            Console.WriteLine(\"Hello World\");\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>If we dig even further into the IL, you can see that it agrees that we are still just making a call to WriteLine() inside of the System.Console class.<\/p>\n<pre>\r\n.method private hidebysig static void Main (\r\n            string[] args\r\n        ) cil managed \r\n    {\r\n        .entrypoint\r\n        IL_0000: nop\r\n        IL_0001: ldstr \"Hello World\"\r\n        IL_0006: call void [mscorlib]System.Console::WriteLine(string)\r\n        IL_000b: nop\r\n        IL_000c: ret\r\n    }\r\n<\/pre>\n<p>Okay, that makes sense.  Seems potentially helpful.  Where do these promised &#8220;gotchas&#8221; come in?  Our issues start to arise when you have multiple &#8220;using static&#8221; declarations in a file.  If those static classes have methods inside them with the same name, conflicts can happen.  Let&#8217;s take a look at one place where this can absolutely happen in a commonly used area of .Net.<\/p>\n<pre class=\"code:csharp\">\r\nusing System.IO;\r\n\r\nnamespace CodeSandbox\r\n{\r\n    class Program\r\n    {\r\n        static string fakeSourceLocation = @\"c:\\\";\r\n        static string fakeBackupLocation = @\"c:\\backup\\\";\r\n\r\n        static void Main(string[] args)\r\n        {\r\n            foreach (var fileWithPath in Directory.GetFiles(fakeSourceLocation))\r\n            {\r\n                var fileName = Path.GetFileName(fileWithPath);\r\n                var backupFileWithPath = Path.Combine(fakeBackupLocation, fileName);\r\n\r\n                if (!File.Exists(backupFileWithPath))\r\n                {\r\n                    File.Copy(fileWithPath, backupFileWithPath);\r\n                }\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>In this simple contrived example, I&#8217;m iterating through all of the files in a directory and if they don&#8217;t exist in a backup location, I copy them there.  This doesn&#8217;t deal with versioning, etc, but it does give us some basic System.IO examples to work with.  Now, if I try to simplify the code like this, I get an issue.<\/p>\n<pre class=\"code:csharp\">\r\nusing static System.IO.Directory;\r\nusing static System.IO.File;\r\nusing static System.IO.Path;\r\n\r\nnamespace CodeSandbox\r\n{\r\n    class Program\r\n    {\r\n        static string fakeSourceLocation = @\"c:\\\";\r\n        static string fakeBackupLocation = @\"c:\\backup\\\";\r\n\r\n        static void Main(string[] args)\r\n        {\r\n            foreach (var fileWithPath in GetFiles(fakeSourceLocation))\r\n            {\r\n                var fileName = GetFileName(fileWithPath);\r\n                var backupFileWithPath = Combine(fakeBackupLocation, fileName);\r\n\r\n                if (!Exists(backupFileWithPath))\r\n                {\r\n                    Copy(fileWithPath, backupFileWithPath);\r\n                }\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>This won&#8217;t even build.  Can you guess why?  If you are very familiar with System.IO classes, you might have noticed that there is a System.IO.File.Exists() and a System.IO.Directory.Exists().  Our build error lets us know by saying, &#8220;The call is ambiguous between the following methods or properties: &#8216;Directory.Exists(string)&#8217; and &#8216;File.Exists(string)'&#8221;.  One way to get around this is to just be explicit at the ambiguous part like this:<\/p>\n<pre class=\"code:csharp\">\r\nusing static System.IO.Directory;\r\nusing static System.IO.File;\r\nusing static System.IO.Path;\r\n\r\nnamespace CodeSandbox\r\n{\r\n    class Program\r\n    {\r\n        static string fakeSourceLocation = @\"c:\\\";\r\n        static string fakeBackupLocation = @\"c:\\backup\\\";\r\n\r\n        static void Main(string[] args)\r\n        {\r\n            foreach (var fileWithPath in GetFiles(fakeSourceLocation))\r\n            {\r\n                var fileName = GetFileName(fileWithPath);\r\n                var backupFileWithPath = Combine(fakeBackupLocation, fileName);\r\n\r\n                if (!System.IO.File.Exists(backupFileWithPath))\r\n                {\r\n                    Copy(fileWithPath, backupFileWithPath);\r\n                }\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>I don&#8217;t know how that makes me feel.  It seems like at least a small code smell.  Maybe in these occasions, you don&#8217;t use this shortcut if it feels wrong.  In general, I could see this being like the Great Var Debate of 2008&trade;.  People thought that <em>var foo = new Bar();<\/em> was smelly compared to <em>Bar foo = new Bar();<\/em>, but it seems like most people have moved past it.  Maybe I&#8217;m just on the beginning of this wave of change and I&#8217;ll stop yelling for the kids to get off of my lawn soon.  At the moment, however, I&#8217;m probably going to file <em>using static<\/em> in the &#8220;not something I&#8217;m going to really use&#8221; pile.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For this next post in my series on the new features in C# 6, I&#8217;m going to cover the using static syntax. This feature is also 100% syntactic sugar, but it can be helpful. Unfortunately, &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137],"tags":[138],"class_list":["post-1053","post","type-post","status-publish","format-standard","hentry","category-c-6","tag-c-6"],"_links":{"self":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/1053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/comments?post=1053"}],"version-history":[{"count":0,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/1053\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/media?parent=1053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/categories?post=1053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/tags?post=1053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}