C# top-level statements vs Main
Answer: By using top-level statements, you can write executable code directly at the root of a file. (S1) The compiler generates the entry point; the explicit Main method still works and is common in existing codebases.
Top-level
// Program.cs
using System;
Console.WriteLine($"Args: {args.Length}");
return 0;
args is available implicitly, and you can await and return an exit code.
Explicit Main
class Program {
static int Main(string[] args) {
Console.WriteLine("Hello");
return 0;
}
}
The runtime calls Main when the app starts. It must be declared inside a class or struct.
Rules
- Only one file per project can have top-level statements.
- Type declarations must come after the statements.
- See Main method entry point for the explicit Main method approach. (S1)
Claims
- id: C1 claim: Use top-level statements for new apps. source: S1 evidence: "Use top-level statements for new apps."
- id: C2 claim: Top-level statements let you write code at the root of a file. source: S1 evidence: "you can write executable code directly at the root of a file."
- id: C3 claim: The Main method is the entry point of a C# application. source: S2 evidence: "The Main method is the entry point of a C# application."
- id: C4 claim: Main must be declared inside a class or struct. source: S2 evidence: "You must declare Main inside a class or struct."
Sources
- [S1] Top-level statements - programs without Main methods - C#. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.
- [S2] Main() and command-line arguments - C#. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/main-command-line . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Microsoft Learn (.NET docs), Top-level statements - programs without Main methods - C#, CC BY 4.0 (dotnet/docs); Microsoft Learn (.NET docs), Main() and command-line arguments - C#, CC BY 4.0 (dotnet/docs).
Cite this
iinventedlight. 2026. "C# top-level statements vs Main." iinventedlight Library, iil:note:zyh5qh3e, version of 2026-11-04. https://iinventedlight.com/library/notes/csharp-top-level-statements/. License: CC-BY-4.0.
@misc{iil_note_zyh5qh3e,
author = {{iinventedlight}},
title = {{C\# top-level statements vs Main}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/csharp-top-level-statements/},
note = {Permanent ID iil:note:zyh5qh3e. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/csharp-top-level-statements.md.