C# 9 features
In this section, we will visit the following new features:
- Top-level statements
- Target-typed
new
expressions - Init-only properties
- Record classes
We will use the top-level statement to simplify some code samples, leading to one code file with less boilerplate code. Then we will dig into the new
expressions that allow creating new instances with less typing. The init-only properties are the backbone of the record classes used in this chapter and are foundational to the MVU example presented in Chapter 18, A Brief Look into Blazor.
Top-level statements (C# 9)
Starting from C# 9, it is possible to write statements before declaring namespaces and other members. Those statements are compiled to an emitted Program.Main
method.
With top-level statements, a minimal .NET "Hello World" program now looks like this:
using System; Console.WriteLine("Hello world!");
Unfortunately, we also need a project to run, so we have...