Skip to main content

How to Use Top-Level Statements in C#

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

In a new console app, Program.cs starts straight with code: no namespace, no class Program, no static void Main. These top-level statements are the program, and the compiler wraps them in an entry point method for you.

Microsoft's guide: Use top-level statements for new apps. By using top-level statements, you can write executable code directly at the root of a file. Behind the scenes: The compiler generates a Program class with an entry point method, and places all your top-level statements in that method.

Syntax​

Syntax
<using directives>

<statements and functions>

<types and namespaces>
PartWhat it is
<using directives>First, when the file has any
<statements and functions>The program, run from top to bottom. args holds the command-line arguments
<types and namespaces>Classes, records and other types, after all the statements

The guide on the order: A file with top-level statements can also contain namespaces and type definitions, but they must come after the top-level statements. And on args: The args variable is never null, but its Length is zero if no command-line arguments were provided.

The Example​

A console app called TopLevel greets the name given on the command line, or world when there's none, and prints how many arguments it got.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n TopLevel
cd TopLevel
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/TopLevel/TopLevel.csproj:
Determining projects to restore...
Restored /Users/you/source/TopLevel/TopLevel.csproj (in 49 ms).
Restore succeeded.

dotnet new console -n TopLevel creates a folder named TopLevel with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or TopLevel.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the statements:

Program.cs (the statements)
// Top-level statements: the program starts on the first line of this file.
string name = args.Length > 0 ? args[0] : "world";

Console.WriteLine(Greet(name));
Console.WriteLine($"Arguments: {args.Length}");

args is there without being declared: it's the array of command-line arguments.

Then a function and a type:

Program.cs (a function and a type)
static string Greet(string who) => $"Hello, {who}!";

// Types come after the top-level statements.
record Visitor(string Name);
  • Greet is a function that the statements call. Microsoft's guide: You can even add functions. The compiler creates them as local functions nested inside the generated entry point method.
  • record Visitor is a type, so it comes after every statement.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Hello, world!
Arguments: 0

macOS-style terminal window titled Terminal — TopLevel showing dotnet run printing Hello, world! and Arguments: 0

Without arguments, args.Length was 0, and the greeting used world.

The Complete File​

macOS-style code window titled Program.cs — TopLevel showing top-level statements that pick a name from args or world and print a greeting and the number of arguments, a static Greet function, and a Visitor record declared after the statements

Program.cs
// Top-level statements: the program starts on the first line of this file.
string name = args.Length > 0 ? args[0] : "world";

Console.WriteLine(Greet(name));
Console.WriteLine($"Arguments: {args.Length}");

static string Greet(string who) => $"Hello, {who}!";

// Types come after the top-level statements.
record Visitor(string Name);

Run It with an Argument​

Put the program's arguments after --:

Terminal
dotnet run -- Asha
Output
Hello, Asha!
Arguments: 1

The dotnet run reference on --: Delimits arguments to dotnet run from arguments for the application being run. All arguments after this delimiter are passed to the application run. Asha arrived as args[0].

Common Mistakes​

MistakeWhat happensDo this instead
A type above the statementsA record before the first statement doesn't build: error CS8803: Top-level statements must precede namespace and type declarations.Move every type below the statements
Top-level statements in a second fileA Second.cs with a Console.WriteLine line broke the build: error CS8802: Only one compilation unit can have top-level statements.Keep the statements in Program.cs, and put only types in other files. The guide: A project can have only one file with top-level statements, but it can have any number of source code files that don't have top-level statements.

FAQ​

Can I still write a Main method?​

You can, but it doesn't run: a Main method in a class named Startup gave warning CS7022: The entry point of the program is global code; ignoring 'Startup.Main()' entry point. The guide: You can explicitly write a Main method, but it can't function as an entry point.

Can I add members to the Program class?​

Yes, with partial class Program: a partial class Program with a Greet method worked, and Program.Greet("Asha") printed Hello, Asha!. A class Program without partial failed with error CS0260: Missing partial modifier on declaration of type 'Program'; another partial declaration of this type exists. The C# specification explains why: The class Program is a partial type declaration ... which is combined with any other partial class declarations for Program within the application.

Conclusion​

With top-level statements, Program.cs is just your code, from the first line: using directives first, then statements and functions, then types. Only one file in a project can have them, and the compiler writes the entry point for you.

Additional Resources​