Welcome to FoxLearn
In this video, I will introduce you to some of the new features in c# 9
First, you need to download .NET 5, then install it to your computer
To practice the demo, you can create a new console application project for .NET Core
Next, You need to change your target framework to .NET Core 5,
then configure your Visual Studio allows you to use previews of the .NET Core SDK.
And don't forget to restart your Visual Studio
To write a simple program in c# requires a remarkable amount of boilerplate code.
This is not only overwhelming for language beginners,
but clutters up the code and adds levels of indentation
You should run the dotnet restore command if you get an error
In c# 9 you can write your main program at the top level
In c# 9 introduces record types,
which are a reference type that provides synthesized methods to provide value semantics for equality
Records are immutable by default
Record types make it easy to create immutable reference types in .NET
There are many advantages to immutable reference types as well
These advantages are more pronounced in concurrent programs with shared data.
Unfortunately, c# forced you to write quite a bit of extra code to create immutable reference types
Records provide a type declaration for an immutable reference type that uses value semantics for equality.
The synthesized methods for equality and hash codes consider two records equal if their properties are all equal
Records support inheritance
You can easily declare a new record derived from Person
Init only setters provide consistent syntax to initialize members of an object
Property initializers make it clear which value is setting which property
The downside is that those properties must be settable
Starting with c# 9, you can create init accessors instead of set accessors for properties and indexers
Callers can use property initializer syntax to set these values in creation expressions,
but those properties are readonly once construction has completed
Init only setters provide a window to change state
That window closes when the construction phase ends
The construction phase effectively ends after all initialization,
including property initializers and with-expressions have completed
Many of the other features help you write code more efficiently
In c# 9, you can omit the type in a new expression when the created object's type is already known
Records support with expressions
A with expression instructs the compiler to create a copy of a record,
but with specified properties modified
In c# 9 includes new pattern matching improvements
One of the most common uses is a new syntax for a null check
Any of these patterns can be used in any context where patterns are allowed
Such as, is pattern expressions, switch expressions, nested patterns,
and the pattern of a switch statement’s case label.
Next, We will create a simple weather forecast class to practice pattern matching
You can create sample data, then use pattern matching to update the Summary property
Several new kinds of patterns have been added in c# 9
So you can now write the weather forecast part of the above pattern as a nested switch expression
You can combine patterns with logical operators and, or and not,
spelled out as words to avoid confusion with the operators used in expressions
Thank you for watching this video and don't forget to subscribe to my channel