What are top-level statements in C# 9.0?
Introduced in C# 9.0, top-level statements remove unnecessary ceremonial code from the program.
To write a simple hello world program, a C# program is written like this:
using System;namespace HelloWorld{class Program{static void Main(string[] args){Console.WriteLine("Hello World!");}}}
The program needs to include multiple lines of code in order to print a single statement.
With top-level statements, the code can be reduced to simply:
using System;Console.WriteLine("Hello World!");
Output
Hello World!
Key points about top-level statements
- It is only possible to include top-level statements in a single file of the application. The compiler will throw an error if it finds top-level statements in multiple source codes.
- It is not possible to use entry-level statements with a
Main()method or program entry point methods. - Top-level statements ease the learning process for beginners. They enable a script-like experience, much like what Jupyter provides for programmers adept in C#.
- A program with top-level statements can contain namespace and type definitions after the top-level statements.
- Top-level statements can also access command-line arguments.
- Top-level statements can call asynchronous methods.
- An integer value returned from a top-level statement can be used as the integer return code of the program.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved