C# 10 Preview: Global Usings and File-Scoped Namespaces

As we approach the .NET 6 release, the C# 10 feature set is solidifying. Two features in particular are going to drastically reduce the vertical “ceremony” code in our files: Global Usings and File-Scoped Namespaces.

File-Scoped Namespaces

The “namespace indentation tax” is finally gone. For 99% of C# files, we only ever have one namespace. Why indent the entire file?

Before (C# 9)

namespace MyCompany.App
{
    public class Service
    {
        // Indented...
    }
}

After (C# 10)

namespace MyCompany.App;

public class Service
{
    // No extra indentation!
}

Global Usings

Every file having `using System;`, `using System.Linq;`, `using Microsoft.AspNetCore.Mvc;` is tedious. Now, you can define these once for the entire project.

I recommend creating a `GlobalUsings.cs` file in the root of your project:

// GlobalUsings.cs
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks;
global using Microsoft.EntityFrameworkCore;

Even better, the .NET 6 SDK automatically generates these for standard namespaces based on your project type (Console, Web, etc.). You can opt-out in csproj if you prefer manual control:

<PropertyGroup>
    <ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>

Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.