C# tutorials > Modern C# Features > C# 6.0 and Later > What are top-level statements in C# 9.0 and what is their purpose?
What are top-level statements in C# 9.0 and what is their purpose?
Understanding Top-Level Statements in C# 9.0
C# 9.0 introduced top-level statements, a feature that simplifies the structure of console applications. This allows you to write simple programs without the boilerplate code traditionally required, such as a Main
method within a class
. This tutorial explains what top-level statements are, their purpose, and how to use them effectively.
Introduction to Top-Level Statements
Top-level statements allow you to write executable code directly in a .cs
file without explicitly defining a Main
method or a surrounding class. The compiler automatically generates the necessary Main
method and class for you. This leads to cleaner and more concise code, especially for simple console applications, scripts, or small utilities. Before C# 9.0, every console application needed at least a class and a Main method.
Basic Example of Top-Level Statements
This is the simplest example of a C# 9.0 program using top-level statements. Instead of the traditional structure, you can directly write the code that needs to be executed. The compiler handles the rest, creating a Main
method implicitly.
Explanation: This single line of code prints "Hello, World!" to the console. The compiler automatically wraps this in a suitable program entry point.
Console.WriteLine("Hello, World!");
More Complex Example with Variables and Methods
This example demonstrates the use of variables and methods within top-level statements. Notice how the methods are defined directly within the file, and are available for immediate use.
Explanation: This code snippet retrieves the username from the environment, prints a greeting, defines a local method PrintMessage
, and then calls this method with another message.
string name = Environment.UserName;
Console.WriteLine($"Hello, {name}!");
void PrintMessage(string message)
{
Console.WriteLine(message);
}
PrintMessage("Welcome to C# 9.0!");
Concepts Behind the Snippet
Main
method for your top-level statements.string[] args
argument to the program, which you can access using the args
identifier.await
in top-level statements if your program requires asynchronous operations.
Real-Life Use Case Section
Top-level statements are excellent for scripting, prototyping, and creating small command-line utilities. Imagine you want to quickly process a file or perform a simple data transformation. Using top-level statements, you can write the script without the verbosity of a full class definition. They are also great for teaching purposes, as they allow beginners to focus on the core logic without getting bogged down in boilerplate code. For example, a simple file reader can be easily implemented with very few lines of code using top-level statements.
Best Practices
Interview Tip
When discussing top-level statements in an interview, highlight their benefits for simplifying code and reducing boilerplate. Be prepared to discuss scenarios where they are most appropriate and their limitations. Also, know that the compiler generates a Program
class with a Main
method under the hood. Showing awareness of the underlying mechanism can be impressive.
When to Use Them
Use top-level statements when:
Alternatives
Alternatives to top-level statements include the traditional class-based structure with a Main
method. For scripting tasks, you could also consider using dedicated scripting languages like PowerShell or Python. LINQPad also offers an environment for writing C# snippets without boilerplate.
Pros
Cons
FAQ
-
Can I use multiple top-level statement files in the same project?
No, only one file in your project can use top-level statements. If you have multiple files with top-level statements, the compiler will generate an error. -
What if I need to access command-line arguments with top-level statements?
The compiler provides an implicitstring[] args
argument to your program, which you can use to access the command-line arguments. For example, you can useif (args.Length > 0) { ... }
to check for arguments. -
Can I return a value from a top-level statement?
Yes, you can return an integer value to indicate the exit code of the application using thereturn
keyword at the end of the top-level statements. For example,return 0;
indicates success, and any other value indicates an error.