With C#, Microsoft has been always some major releases: with a lot of features up, and less frequent release. They went ahead than routinely, omitting the conventional “.0” when they talked about C# 6.0!

C# 7.1

In the C# 7.0, they are trying something innovative. Tools like Visual Studio upgrade in a frequent mode and they left no reason why the C# couldn’t also be updated more frequently. Hence this time, they are embracing a notion of “point releases”; minor versions of C# with quite useful but smaller language features and with shorter intervals. This indicates that you don’t need to wait for a long time to make some additional value to ship, but also it becomes easy to align a C# release with the shipping of related features, e.g. in.NET.

It is a fact that upgrading to a newer version of programming language every time could be a hassle in an organization and is not the way for everyone. Visual Studio 2017 allows you to decide whether to take the latest or only to the major (“.0”) versions of C#. You can choose your own rhythm.

The first point release

In August of 2017 Microsoft shipped the first point release of C# and named as C# 7.1, and its main purpose is to get practice with point releases, without too many dependencies on accompanying technology to make things complicated.

C# 7.1 is a small release with just a few and well-chosen new language attributes but in the minor range. It’s supported by VS 2017, starting with Update 15.3.

Let’s take a look! There is a program which uses the three C# 7.1 features that are being covered here, and a number of the recent C# 7.0 ones.

using System.Linq;

using System.Threading;

using System.Threading.Tasks;

using static System.Console;

class Program

{

static async Task Main(string[] args)

{

var results = Enumerable.Range(1, 40)

.Select(input => (input, task: FibonacciAsync(input)))

.ToArray();

for each (var tuple in results)

{

WriteLine($”Fib {tuple.input} = {await tuple.task}”);

}

}

private static Task<int> FibonacciAsync(int n, CancellationToken token = default)

{

return Task.Run(() => Fib(n).curr, token);

(int curr, int prev) Fib(int i)

{

if (i is 0) return (1, 0);

var (c, p) = Fib(i – 1);

return (c + p, c);

}

}

}

view raw

cs7.1-Example.cs hosted with by GitHub

It figures out the first 40 Fibonacci numbers in parallel on the thread pool and prints them out in order.

Here let’s take a look at C# 7.1 features used here.

Async Main

The Main entry point method can return a Task or a Task<int>. And as a result, execution will wait for the returned task to complete, before shutting down the program.

The main purpose is to make the Main method async, which you can do as well:

static async Task Main(string[] args)

This makes you wait for the Main method, which you couldn’t do before.

WriteLine($”Fib {tuple.input} = {await tuple.task}”);

Previously what you need to do was that, first you’d create an async helper method, for instance, MainAsync, with all the logic in. Then you can write this cryptic Main method:

static void Main(string[] args) => MainAsync().GetAwaiter().GetResult();

Now you can asynce the Main method and the compiler would rewrite it for you.

Inferred tuple element names

In lambda expression in the query:

input => (input, task: FibonacciAsync(input))

Only a tuple is created, and give a name, task, for the second element. And a few lines later you are able to say

WriteLine($”Fib {tuple.input} = {await tuple.task}”);

To access the first element with the name tuple.input.

Default literals

With an expected type for a default expression, you can omit the mention of the type, as done for the CancellationToken in the signature of the FibonacciAsync method:

Private static Task<int> FibonacciAsync(int n, CancellationToken token = default)

This prevents a repetition of type names or typing the longer ones which are already given by context.

Microsoft is already working on C# 7.2, and also the features intended for their next major release.

With this, we conclude.

Happy coding!

If you want to enhance yourself in Dot Net Course and improve yourself through Dot NET training program; our institute, CRBtech Solutions would be of great help and support. We give a well-structured program for the best Dot Net Course. Among many reputed institutes of dot net training and placement in Pune, CRB Tech has created a niche for itself.