Top-Level Programs is a new feature being introduced in C# 9.0 that significantly simplifies certain C# programs and allows you to remove almost all of the boilerplate code from them. This pattern allows you to call functions at the top-level of your C# program meaning that you do not need a class or function definition in order to call those functions. Instead C# automatically generates that code for you when your code is compiled.
The simplest top-level program is just one line:
There are a few special features included with top-level programs that help you to fully use some of the features like accepting command line arguments or async functions.
C#’s top-level programs include a magic
args variable which contains a
string of command-line arguments provided. Here is a working example script that accepts a command line argument to greet you by name.
dotnet run -- "World of Zero" or similar to run this code and be greeted with “Hello, World of Zero!”
You may take advantage of async functions inside of a top-level program. This allows for things like async file operations, web requests etc to be made without having to wrap them in a separate async function.
For example this task will print two numbers with a short pause between them.
More info about some of the new features in C# 9.0 and how to use them is available here: https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u