C# 9.0 The Main New Features Explained in Less Than 5 Minutes
Here’s a quick reminder of some of the new features of C# 9.0
--
C # 9 is officially released with .NET 5 on November 2020. In this article, I will introduce some of the new features that I think are interesting to know.
Top-Level Statements
In C# 9, you can write your main program at the top level, immediately after using
statements like the following code:
With top-level statements, there is no need to declare any namespace, class Program
or main
method. The compiler does all these stuff for you. Let’s use SharpLab to see the code generated by the compiler:
This new feature can be useful for novice programmers who want to learn the fundamentals of C #.
Target-Typed new
In C#9, you can omit the type in new
expression when the object type is explicitly known.
This new feature is a syntactical sugar to read clean code without duplicating the type.
Lambda Discard Parameters
In C# 9, you can use discard(_
)as an input parameter of a lambda expression if this parameter is unused.
This feature is another syntactical sugar, our code is more clean and sweet.
Records
In C# 9, we have a new reference type called record
that provides value equality:
Point record
is immutable, Its properties are read-only, you can simplify this syntax by using init
…