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

Abdelmajid BACO
4 min readNov 28, 2020

--

C# 9.0 comes with very interesting new features like record type, top-level statements and so on.
image from https://github.com/dotnet

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:

Top-Level Statements
SharpLab generated code

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.

Target-Typed new
Target-Typed and object initializer Syntax
Target-Typed new
Target-Typed and constructor

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:

Record type
Record type

Point record is immutable, Its properties are read-only, you can simplify this syntax by using init

--

--

Abdelmajid BACO

Senior Full Stack .Net / Angular Developer, Cloud & Azure DevOps, Carrier Manager, Husband, and Father.