TechBubbles Microsoft Technology BLOG

C# 7 features overview

C# 7 added some new features with a focus on code simplification and performance. This post explains C# 7 new features

Binary Literals

literals

 

 

 

 

We now have binary literals like 0b followed by 0’s and 1’s; literals can often get longer so you can add underscore to separate them as shown in above screenshot.

Tuples

Install-Package “System.ValueTuple”

One of the major features of the C# 7 is Tuples. Assume you have Tally function to compute sum and count for the numbers you supply. We want to have two results from the function. Using Tuples you can declare Tally function as follows

tally

 

 

 

 

 

 

 

 

 

 

The return type in above function is tuple type with two other types in the parenthesis. The real implementation of the above method is as follows

tuples

 

 

 

 

 

 

 

 

 

Above method loop through the all values and each time around it updates the result tuple in r. Tuples are structs i.e. value types and they are created locally and passed by copying the content. Tuples are in fact mutable and elements are simply public mutable fields.

Local Functions

Assume if you want to move the above looping logic to Add method then you can do as follows

local functions

 

 

 

 

 

 

 

Add method is inside your Tally method. This is called local function because it has access to all local variables of the enclosing scope and it can be used inside the scope it is declared in.

Pattern matching

Patterns are new kinds of construct in C# that can be used to testing the values in various ways. Among other places patterns can be used in if expressions.

pattern

 

 

 

 

 

 

Patterns can also be used in switch statements, now you can switch on anything J it’s no longer limited to primitive types.

switch

 

 

 

 

 

More on this topic can be read here   and https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/

 

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

Add Comment

TechBubbles Microsoft Technology BLOG

Follow me

Archives

Tag Cloud