After 18 years of existence, C# is still as young and "sharp" as ever. Indeed, by open-sourcing its compiler and design, Microsoft strengthened the position of C# in the market by making it available to all developpers on GitHub.
Contributing and voting to set the language roadmap are the main contributors to its success in the last few years.
Last month, the C# language team in Microsoft announced the release of the preview version of VS 2019 and .NET Core 3.0.
The exciting things about it is that this version includes the new C# 8 that we can play with to test some of the new features.
In this article, I'll walk you through some of the new features introduced in the 8th version of C# and show some use cases.
Ranges and Indices
C# 8 introduces two new types and operators for collections manipulation and indexing. Basically we will have a more interesting and elegant way to index and slide collections.
New types:
System.Index
and System.Range
.
New Operators :
..
and ^
.
Let's see some examples:
The element selection is 0-based if you are counting from the beginning and 1-based if you are counting from the end.
- Get the last element:
- Get a range of elements:
Limitation:
This new feature will be available in any framework implementing .NET Standard 2.1. For example, it will be available in .NET Core 3.0 but not in the .NET framework 4.8. If you want more informations about .NET Standard you can check this article.
Default Implementations of Interface Members
For this part, we will imagine that we have a three year old application with a bunch of classes and interfaces, and that 30 of these classes them implement an Interface called
IPrintable
.
This interface includes a signature for each printer operation.
Let's say you want to add a new method that does exactly the same thing as
Print
, but which has a different signature. In this case, there is no way around reimplementing the new method for all 30 classes.
With C# 8, you can define a default implementation for the new interface member.
Let's check how this works.
Switch Expressions
We can say that this is a light or short version of a switch statement with a new syntax using the
=>
operator.New Target-Typed Expressions
To build a collection with typed objects we are obliged to specify the type for each element or use the new statement for the object construction.
This is an example:
With the new target-typed expression there's no need to specify the object type or class name we can do like this:
Async Streams
C# 5 made it possible to have some asynchronous features like awaiting a result with
await
and async
. In C# 8, we get an evolution of this feature adding the IAsyncEnumerable
which allows us to yield results asynchronously.
This feature can be very useful if you are developing an IoT app and you are dealing with a lot of asynchronous calls returning data.
Here is an example:
We also need to add async to our
Main
method to make it workNullable Reference Types
As we all know there are two* types in C# : reference type and value type.
struct
is a typical example of a value type while class
is a reference type. If you want more information about those two check this link.
In this section, we will talk about the famous, the one, the only null pointer exception and how to prevent it from happening.
It's too simple — you need just to activate the null reference types feature in your project and you will get a warning if you are doing something wrong.
For example, if you are doing something like:
You will get a first warning saying "Assigning null to a non-nullable type."
We will fix it by changing string to string and checking if the name is null with ??.
You can get his type of warning if you are Resharper too :p
* C# includes 3 types not 2 . I discovered the third one this week it's called "Pointer Type". I've never used this type but it looks like the C/C++ pointer.
No comments:
Post a Comment