Home Whats new in C# 7.0
Post
Cancel

Whats new in C# 7.0

Recently I made a post, about the new features in C# 6.0 and in this post I will show you whats new in C# 7.0.

Whats new in C# 7.0

To use all features of C# 7.0 you have to use Visual Studio 2017. In theory, you can also use Visual Studio 2015 but it’s a pain to get it running. I highly recommend using Visual Studio 2017

out Parameter

Before C# 7.0 the variable used as out parameter needed to be declared. With C# 7.0 you don’t have to do that anymore which makes the code shorter and more readable.

new in C# 7.0 the out parameter

The new out parameter

It’s not a big change but it’s a nice simplification of the code.

Tuples

Tuples give you the possibility to return two values at a time. These two values can be different data types. On the screenshots below you can see the call of a simple method which returns two strings.

Tuples call

Call of the method which returns a tuple value

 

Tuples return

Method returns a tuple value

To get access the returned values use variable.returnName. The returnName is the name you defined in the signature of the method.

To use Tuples you need to target the .NET Framework 4.7. If you target a lower .NET Framework, you have to install the System.ValueTuple NuGet package which can be found here.

Pattern matching

With the new feature pattern matching it is now possible to have switch cases for data types. For example case int: do something, case string: do something else. On the screenshot below I show how to calculate the sum of all int and double values of a list of objects. If the element is a string the program writes the string to the console.

pattern matching list

List of objects containing different data types

 

pattern matching

Switch statement for pattern matching calculating the sum of int and double values

Literal improvements

The last new feature I want to talk about is the literal improvement. With C# 7.0 it is now possible to separate const numbers with an underscore to improve the readability.

Literal improvements

Better readability of long numbers

These numbers can be used as normal numbers without an underscore which means when printed the underscore won’t be printed.

Code and further reading

You can find the code examples on GitHub. A more extensive post about whats new in C# 7.0 can be found in the official documentation.

This post is licensed under CC BY 4.0 by the author.

Web sites cannot be started unless WAS and the World Wide Web Publishing Service are running

Visitor Pattern

Comments powered by Disqus.