

C# 9 top-level programs and target-typed expressions
source link: https://developers.redhat.com/blog/2021/03/30/c-9-top-level-programs-and-target-typed-expressions/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C# 9 top-level programs and target-typed expressions


.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.
Before we get started: Last year, we published a series about C# 8. You can find the articles here:
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.
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.
Related
Recommend
-
48
Starting August 2018, all new apps submitted to the Play Store must target API level 26. Starting November 2018, updates to existing apps must target API 26
-
25
So far we have talked about how quite a few new features of C# 8. We have talked about how C# 8 fixing interpolated verbatim strings (here), we...
-
13
Playing with C#9 Top-level Programs, Records and Elasticsearch.NET This post is for C# Advent Calendar 2020 organized by Matthew Groves. I...
-
11
Top-level programs in C# 9.0C# 9.0 comes with nice new feature called top-level programs. It’s something that teachers of beginner classes will love for sure. Imagine – you start teaching C# with only two lines of code on screen. Al...
-
9
How to use configuration with C# 9 top-level programs In this post, let's use C# 9 top-level programs to read from configuration....
-
8
Description In this quick video, Cecil and Jayme will teach us about the new target-typed new expres...
-
6
Beginners Level: Akka Typed API Knoldus Blog Audio Reading Time: 3 minutes
-
11
Typed Programs Don't Leak Data In which we turn privacy violations into compile-time errors in a simple imperative language embedded in Haskell and enforce it in style using GADTs. Private data should remain private. The g...
-
2
Target Typed New Expressions in C# 9 - Are They The Future? Initial thoughts about target typed new expressions in C#9. Are they good? Are they bad? Does "it depend" on the scenario? Let's take a look. ...
-
7
ICYMI C# 9 New Features: Reducing Code with Target-typed New ExpressionsThis is part of a series of articles on new features introduced in C# 9. C# 9 introduced some enhanc...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK