Back to results for “Fundamentals”
C# › Modern C# Features (Global Usings, File-Scoped Namespaces, Top-Level Statements)

What is a file-scoped namespace declaration, and how does it reduce indentation compared to a traditional block-scoped namespace?

Beginner

Answer

A file-scoped namespace (`namespace MyApp.Services;` with a semicolon, no braces) applies to EVERY type declared in the rest of that file, eliminating one level of nested indentation compared to the traditional `namespace MyApp.Services { ... }` block-style syntax — purely a readability/formatting improvement with no behavioral difference.
// Modern: file-scoped namespace, less indentation
namespace MyApp.Services;

public class OrderService { }

// Old: block-scoped namespace, extra indentation level
namespace MyApp.Services {
  public class OrderService { }
}
Real-world example Reducing unnecessary indentation across an entire codebase when every file only ever contains ONE namespace anyway.

Common follow-ups: Can a single file have MULTIPLE file-scoped namespace declarations?