Skip to content

C# File-Scoped Namespace — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about C# File. We cover key concepts, practical examples, and best practices.

Every C# file starts with a namespace block that indents the entire file. After four or five files, you have hundreds of wasted indentation levels. File-scoped namespaces eliminate the block and the extra indentation.

Wrong

namespace MyApp.Services
{
    public class UserService
    {
        public void DoSomething()
        {
            // Indented twice before any code
        }
    }
}

Output: Works, but every type inside the namespace is indented an extra level. When the file is 200 lines, that is 200 unnecessary leading spaces.

namespace MyApp.Services;

public class UserService
{
    public void DoSomething()
    {
        // No unnecessary indentation
    }
}

Output: Same compiled result. The ; after the namespace declaration tells the compiler every type in this file belongs to that namespace.

A file can only have one file-scoped namespace. If you need nested namespaces or multiple, use the traditional block syntax.

Prevention

  • Use file-scoped namespaces in all new C# files — they are the standard in .NET 6+.
  • Use a single namespace per file (which you should do anyway).
  • Keep the namespace line at the top, after using directives.
  • Use traditional block namespace only when a file contains types in multiple namespaces (rare).
  • Configure your editor to use file-scoped namespaces by default.
  • Ensure your project targets .NET 6+ (or uses C# 10+).

Common Mistakes with file scoped namespace

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world CSHARP code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Can I use both file-scoped and block namespaces in the same project?

Yes. Each file can independently choose its style. The compiler does not care. However, pick one style per project for consistency. File-scoped is the recommended default.

What happens if I forget the semicolon?

You get a compiler error. The ; is required to distinguish file-scoped from block namespace. The error message is usually CS1513: } expected or similar — the compiler tries to interpret the file as block syntax.

Can I still have usings inside the namespace?

Yes, but with file-scoped namespaces, all using directives are file-scoped (they go at the top of the file). You cannot have namespace-scoped using directives because there is no block. This is fine — file-scoped usings are the common case.

File-scoped namespaces are the default in all DodaTech projects using .NET 6+. For more C# practices, visit DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro