{"id":3271,"date":"2016-05-04T08:07:47","date_gmt":"2016-05-04T08:07:47","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=3271"},"modified":"2016-05-04T08:07:47","modified_gmt":"2016-05-04T08:07:47","slug":"c-6-features","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/c-6-features\/","title":{"rendered":"C# 6 features"},"content":{"rendered":"<p>A look at some of the new C# 6 features (not in any particular order).<\/p>\n<p><strong>The Null-conditional operator<\/strong><\/p>\n<p>Finally we have a way to reduce the usual<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nif(PropertyChanged != null)\r\n   PropertyChanged(sender, propertyName);\r\n<\/pre>\n<p>to something a little more succinct<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nPropertyChanged?.Invoke(sender, propertyName);\r\n<\/pre>\n<p><strong>Read-only auto properties<\/strong><\/p>\n<p>In the past we&#8217;d have to supply a private setter for read only properties but now C# 6 allows us to do away with the setter and we can either assign a value to a property within the constructor or via a functional like syntax, i.e.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MyPoint\r\n{\r\n   public MyPoint()\r\n   {\r\n      \/\/ assign with the ctor\r\n      Y = 10;\r\n   }\r\n\r\n   \/\/ assign the initial value via the initializers\r\n   public int X { get; } = 8;\r\n   public int Y { get; }\r\n}\r\n<\/pre>\n<p><strong>Using static members<\/strong><\/p>\n<p>We can now &#8220;open&#8221; up static class methods and enums using the<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing static System.Math;\r\n\r\n\/\/ Now instead of Math.Sqrt we can use\r\nSqrt(10);\r\n<\/pre>\n<p><strong>String interpolation<\/strong><\/p>\n<p>Finally we have something similar to PHP (if I recall my PHP from so many years back) for embedding values into a string. So for example we might normally write String.Format like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar s = String.Format(&quot;({0}, {1})&quot;, X, Y);\r\n<\/pre>\n<p>Now we can instead write<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar s = $&quot;({X}, {Y})&quot;;\r\n<\/pre>\n<p><strong>Expression-bodied methods<\/strong><\/p>\n<p>A move towards the way F# might write a single line method we can now simplify &#8220;simple&#8221; methods such as <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic override string ToString()\r\n{\r\n   return String.Format(&quot;({0}, {1})&quot;, X, Y);\r\n}\r\n<\/pre>\n<p>can now be written as<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic override string ToString() =&gt; String.Format(&quot;({0}, {1})&quot;, X, Y);\r\n\r\n\/\/ or using the previously defined string interpolation\r\npublic override string ToString() =&gt; $&quot;({X}, {Y})&quot;;\r\n<\/pre>\n<p><strong>The nameof expression<\/strong><\/p>\n<p>Another improvement to remove aspects of magic strings, we now have the nameof expression. So for example we might have something like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic void DoSomething(string someArgument)\r\n{\r\n   if(someArgument == null)\r\n      throw new ArgumentNullException(nameof(someArgument));\r\n\r\n   \/\/ do something useful\r\n}\r\n<\/pre>\n<p>Now if we change the <em>someArgument<\/em> variable name to something else then the nameof expression will correctly pass the new name of the argument to the ArgumentNullException.<\/p>\n<p>However nameof is not constrained to just argument in a method, we can apply nameof to a class type, or a method for example.<\/p>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/channel9.msdn.com\/Series\/Visual-Studio-2015-Enterprise-Videos\/Whats-New-in-CSharp-6#time=0s\" target=\"_blank\">What&#8217;s new in C# 6<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A look at some of the new C# 6 features (not in any particular order). The Null-conditional operator Finally we have a way to reduce the usual if(PropertyChanged != null) PropertyChanged(sender, propertyName); to something a little more succinct PropertyChanged?.Invoke(sender, propertyName); Read-only auto properties In the past we&#8217;d have to supply a private setter for read [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3271","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=3271"}],"version-history":[{"count":10,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3271\/revisions"}],"predecessor-version":[{"id":3910,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3271\/revisions\/3910"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=3271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=3271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=3271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}