---
source: https://iinventedlight.com/library/notes/csharp-top-level-statements.md
title: "C# top-level statements vs Main"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC BY 4.0 (dotnet/docs); prose restates cited source lines"
attribution: "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)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: C#"
tags: ['csharp', 'dotnet', 'program-structure']
sources:
  - id: S1
    title: "Top-level statements - programs without Main methods - C#"
    publisher: Microsoft Learn (.NET docs)
    url: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements
    accessed: 2026-11-04
    license: "CC BY 4.0 (dotnet/docs)"
  - id: S2
    title: "Main() and command-line arguments - C#"
    publisher: Microsoft Learn (.NET docs)
    url: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/main-command-line
    accessed: 2026-11-04
    license: "CC BY 4.0 (dotnet/docs)"
---
# 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

```csharp
// 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

```csharp
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).
