Global usings

Organize `using` statements so they're out of your way.

Global usings are designed to minimize boilerplate code and enhance readability in your application.

Global usings

More often than not, developers find themselves using the same namespaces such as System.Linq, System.Collections.Generic, and Microsoft.Extensions.DependencyInjection repetitively across .cs files. Every new class file you create begins with an almost identical few lines of using declarations. But with global using directives, we can now consolidate these common namespaces in a single file. This results in a cleaner, consistent, and more organized codebase.

Global usings are available in C# 10 and later, and can be configured in the .csprog file by settings the ImplicitUsings flag, like so:


When ImplicitUsings is set, the compiler creates a file GlobalUsings.g.cs in the \obj\Debug\net8.0\ folder. You can choose which namespaces you'd like to include by modifying the .csproj file and adding them under an ItemGroup.

    <Using Include="MyProject.Namespace"/>

The GlobalUsings.g.cs file looks like the following:

// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;

Notice the syntax global using global::. This is so the compiler can figure things out so there are no namespace clashes, and only the specific namespaces listed are included (but not their sub-namespaces).

Refactor to use Global usings

In Rider, to refactor to use global usings, click on the light bulb 💡 or press Alt+Enter on the using statement. Choose Convert to global using Rider presents a dialog where you can choose which namespaces to move. Select the ones you want and click Next This refactoring removes all using statements that are to be converted to a global using from their original files.

This refactoring converts those repeated using directives for into a single global using directive in the GlobalUsings.cs file (or the name you choose). The GlobalUsings.cs file should look similar to the following, but with your namespaces you're using:

// Global using directives
global using Microsoft.EntityFrameworkCore;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Logging;

Global usings also support static usings and using aliases. Ultimately, the global using feature in C# helps trim down code, making it more readable and maintainable.

Related Resources

File-scoped namespaces and types
File-scoped namespaces and types
Organize code better and reduce bugs by using file-scoped namespaces and objects.
C# Top-level statements
C# Top-level statements
Use top-level statements in C# to reduce boilerplate code.
Use collection initializers
Use collection initializers
Update and initialize items in old collection declarations.