.NET 5 (released in November 2020) includes support for C# 9, a major new version of the C# programming language. This series of articles explores the new features in .NET’s main programming language. In this first article, we’ll look at top-level statements and target-typed new and conditional expressions. These features make C# less verbose and can be used in everyday programs.
Read the whole series
Read the other articles in this series introducing new features in C# 9:
- Part 2: C# 9 pattern matching
- Part 3: C# 9 new features for methods and functions
- Part 4: C# 9 init accessors and records
- Part 5: Some more C# 9
Additionally, last year, we published a series about C# 8. You can find those articles here:
- Part 1: C# 8 asynchronous streams
- Part 2: C# 8 pattern matching
- Part 3: C# 8 default interface methods
- Part 4: C# 8 nullable reference types
- Part 5: Some more C# 8
Top-level programs
Top-level programs allow you to write the main method of your application without having to add a class
with a static Main
method. For example:
// using directives using static System.Console; using System.Threading.Tasks; // program statements await Task.Delay(100); WriteLine("Hello " + (args.Length > 0 ? args[0] : "world!")); return 0; // local functions // class/namespace declarations void Foo() { } class Foo { }
The program statements are added directly in the C# file without an enclosing method, class, or namespace. You can place using
directives before the program statements. Optionally, after the statements, you can define local functions, types, and namespaces.
The example also shows some interesting features of top-level programming. The program can be asynchronous: we can use the await
keyword. Program arguments are available using the args
parameter, and the program may return an exit code.
Target-typed new expressions
Since C# 3, we can omit the declaring type for variables using the var
keyword. The compiler derives the type from the expression:
var person = new Person();
With C# 9, you can also omit the type from the new
operator, and make the compiler derive the type from the declaring type:
Person p1 = new(); Person p2 = new("Tom"); Person p3 = new() { FirstName = "Tom" };
The benefit of this syntax is that the type declarations are nicely aligned on the left-hand side. As you can see in the example, you can pass constructor arguments and use object initializers.
Target-typed new
expressions also work when you’re passing an argument to a method. However, it is less clear what type is constructed:
PrintPerson(new());
Target-typed conditional expressions
With C# 9, the branches of ? .. : ..
expressions are allowed to have different types, as long as both of them convert to the target type:
Control c = true ? button : form;
This example works in C# 9 even though button and form are of different types, because both convert to the target type (Control
). Previously, the branches needed to have the exact same type, which required introducing casts when they didn’t match.
Conclusion
In this article, we looked at top-level programs, which make writing the main method less verbose. We also covered target-typed new expressions, which provide a nice syntax for aligning the types of variable declarations without having to duplicate the type for the new
operator. And finally, we saw how target-typed conditional expressions allow us to omit casts when both branches convert to the target type.
In the next article, we'll explore new features for pattern matching in C# 9.
You can use C# 9 with the .NET 5 SDK, which is available on Red Hat Enterprise Linux, Red Hat OpenShift, Fedora, Windows, macOS, and other Linux distributions.
Last updated: April 28, 2021