← All notes

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

Claims

Sources

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

Permanent ID: iil:note:zyh5qh3e

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.},
}

Markdown

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.