{"id":15835,"date":"2017-10-31T15:39:48","date_gmt":"2017-10-31T22:39:48","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/dotnet\/?p=15835"},"modified":"2021-09-29T16:38:00","modified_gmt":"2021-09-29T23:38:00","slug":"welcome-to-c-7-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/welcome-to-c-7-1\/","title":{"rendered":"Welcome to C# 7.1"},"content":{"rendered":"<p>With C# we have always tended towards major releases: bundle a lot of features up, and release less frequently. We even went so far as routinely omitting the traditional &#8220;.0&#8221; when we talked about C# 6.0!<\/p>\n<p>In the C# 7.0 &#8220;wave&#8221; we are trying something new. Tools such as Visual Studio upgrade on a frequent cadence, and there&#8217;s no longer a technical reason why C# couldn&#8217;t also be updated more frequently. So this time around we are embracing a notion of &#8220;point releases&#8221;; minor versions of C# that trickle out useful but smaller language features with shorter intervals. This means that you don&#8217;t have to wait so long for additional value to ship, but also makes it easier to align a C# release with the shipping of related features, e.g. in .NET.<\/p>\n<p>Of course, upgrading to a new version of the language &#8220;all the time&#8221; can be a hassle in an organization, and isn&#8217;t for everyone. Visual Studio 2017 lets you decide whether to snap to the latest, or only to major (&#8220;.0&#8221;) versions of C#. You can choose your cadence.<\/p>\n<h2>The first point release<\/h2>\n<p>In August of 2017 we shipped the first point release of C#. It&#8217;s called C# 7.1, and its main purpose is really for us to get practice with point releases, without having too many dependencies on accompanying technology to complicate matters.<\/p>\n<p>C# 7.1 is therefore a small release with just a few (but well-chosen) new language features; useful we think, but definitely in the minor range. It&#8217;s supported by <a href=\"https:\/\/www.visualstudio.com\/downloads\/\">Visual Studio 2017<\/a>, starting with <a href=\"https:\/\/blogs.msdn.microsoft.com\/visualstudio\/2017\/08\/14\/visual-studio-2017-version-15-3-released\/\">Update 15.3<\/a>.<\/p>\n<p>Let&#8217;s have a look! Here&#8217;s a program that uses the three C# 7.1 features we&#8217;re going to cover, plus a number of the recent C# 7.0 ones, just to make it interesting.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/MadsTorgersen\/8ce4ea8557d9ca502dc6196121eb60c4.js\"><\/script><\/p>\n<p>It computes the first 40 Fibonacci numbers in parallel on the thread pool, and prints them out in order.<\/p>\n<p>Let&#8217;s look at each of the new C# 7.1 features used in here. For a full overview of the features in C# 7.1, check out the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/whats-new\/csharp-7-1\">docs<\/a>.<\/p>\n<h2>Async Main<\/h2>\n<p>The <code>Main<\/code> entry point method can now return a <code>Task<\/code> or a <code>Task&lt;int&gt;<\/code>. When it does, execution will wait for the returned task to complete, before shutting down the program.<\/p>\n<p>Of course, the common use of this will be to make the <code>Main<\/code> method <code>async<\/code>, which you can also do:<\/p>\n<pre class=\"lang:c# decode:true\">\r\n    static async Task Main(string[] args)\r\n<\/pre>\n<p>This lets you <code>await<\/code> directly in the <code>Main<\/code> method, something you couldn&#8217;t do before.<\/p>\n<pre class=\"lang:c# decode:true\">\r\n    WriteLine($\"Fib {tuple.input} = {await tuple.task}\");\r\n<\/pre>\n<p>What you had to do previously was quite unappetizing: first you&#8217;d create an async helper method, <code>MainAsync<\/code>, say, with all the logic in. Then you&#8217;d write this cryptic <code>Main<\/code> method:<\/p>\n<pre class=\"lang:c# decode:true\">\r\n    static void Main(string[] args) =&gt; MainAsync().GetAwaiter().GetResult();\r\n<\/pre>\n<p>Now you can just make your <code>Main<\/code> method async, and the compiler will rewrite it for you.<\/p>\n<h2>Inferred tuple element names<\/h2>\n<p>In this lambda expression inside the query:<\/p>\n<pre class=\"lang:c# decode:true\">\r\n    input =&gt; (input, task: FibonacciAsync(input))\r\n<\/pre>\n<p>You notice that we create a tuple, but only give a name, <code>task<\/code>, for the second element. Yet a few lines later we are able to say<\/p>\n<pre class=\"lang:c# decode:true\">\r\n    WriteLine($\"Fib {tuple.input} = {await tuple.task}\");\r\n<\/pre>\n<p>Accessing the first element by the name <code>tuple.input<\/code>. That&#8217;s because when you create a tuple with an expression that &#8220;has&#8221; a name, like <code>input<\/code> in the lambda expression above, we&#8217;ll now automatically give the corresponding tuple element that name.<\/p>\n<h2>Default literals<\/h2>\n<p>If there&#8217;s an expected type for a <code>default<\/code> expression, you can now omit the mention of the type, as we do for the <code>CancellationToken<\/code> in in the signature of the <code>FibonacciAsync<\/code> method:<\/p>\n<pre class=\"lang:c# decode:true\">\r\n    private static Task&lt;int&gt; FibonacciAsync(int n, CancellationToken token = default)\r\n<\/pre>\n<p>This avoids tedious repetition of type names, or typing out long ones when they are already given by context.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>We are already working on C# 7.2, as well as features that are intended for the next major release. If you are curious, you can follow along and participate at the C# language design GitHub repo: <a href=\"https:\/\/github.com\/dotnet\/csharplang\">github.com\/dotnet\/csharplang<\/a>.<\/p>\n<p>Happy hacking!<\/p>\n<p><strong>Mads Torgersen<\/strong>, Lead Designer of C#<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With C# we have always tended towards major releases: bundle a lot of features up, and release less frequently. We even went so far as routinely omitting the traditional &#8220;.0&#8221; when we talked about C# 6.0! In the C# 7.0 &#8220;wave&#8221; we are trying something new. Tools such as Visual Studio upgrade on a frequent [&hellip;]<\/p>\n","protected":false},"author":1379,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685],"tags":[30,36,46,147],"class_list":["post-15835","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","tag-announcement","tag-async","tag-c","tag-visual-studio"],"acf":[],"blog_post_summary":"<p>With C# we have always tended towards major releases: bundle a lot of features up, and release less frequently. We even went so far as routinely omitting the traditional &#8220;.0&#8221; when we talked about C# 6.0! In the C# 7.0 &#8220;wave&#8221; we are trying something new. Tools such as Visual Studio upgrade on a frequent [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/15835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/1379"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=15835"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/15835\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=15835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=15835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=15835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}