Glossary / File-scoped namespaces
File-scoped namespaces
A file-scoped namespace declares that all following type declarations in a source file belong to one namespace, using a terminating semicolon instead of braces. It reduces indentation and boilerplate but does not change runtime namespace semantics or allow multiple file-scoped namespace declarations in one file.
Syntax and constraints
namespace Company.Product;
public sealed class Service { }
The declaration must appear before type declarations (after permitted extern/using directives and attributes as defined by grammar). A file can use either block-scoped or file-scoped namespace syntax, not both for namespace declarations in the same file. Nested namespaces can be written with dotted names. using scope and global using behavior remain governed by their own rules.
File-scoped namespaces were added in C# 10; projects must use a supporting compiler/language version. Source generators and code formatters may prefer one style consistently. Namespace names are source organization and metadata identity, not necessarily a match to folder path.
Interview questions
- Does file-scoped change type identity? No; it changes syntax only.
- Can a file contain two file-scoped namespaces? No, one file-scoped namespace declaration per file.
- Can block and file-scoped forms mix? Not as namespace declarations in the same file.
- What version introduced it? C# 10.
- Does folder path determine namespace? No; namespace is declared explicitly or configured by tooling conventions.