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
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.
It’s not a big change but it’s a nice simplification of the code.
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.
Call of the method which returns a tuple value
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.
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.
List of objects containing different data types
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.
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.