Global Using Directives

C# 10.0 .NET 6.0

Published Updated Author Jeffrey T. Fritz Reading time

Global using directives allow you to specify imports that automatically apply to all files in your project, reducing repetitive using statements and improving code organization at scale.

Introduced in C# 10.0, global using directives enable you to declare imports once that apply across your entire project. Instead of repeating the same using statements in every file, you centralize common namespaces (and static types) in a single location, typically in a dedicated Usings.cs file or Program.cs.

Why it matters

Global using directives significantly reduce boilerplate in large projects. They’re especially valuable in API projects where System.Text.Json, logging, and other standard dependencies are used across every file. This improves consistency, reduces line clutter in individual files, and makes onboarding new developers easier by centralizing project-wide import conventions.

Cautions

While convenient, global using directives can obscure dependencies if overused. It’s best to keep them limited to truly universal namespaces (System, common logging, dependency injection) and avoid adding project-specific or domain-specific imports globally. Be aware that global using statements apply to all files, which can make code less self-documenting compared to explicit local imports.

Using global directives

Global using directives apply to all files in your project, eliminating the need to repeat common imports in every file.

Valid since C# 10.0

// Usings.cs (or Program.cs) - define global using statements
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Text.Json;
global using System.Threading.Tasks;

// Now any other file in the project can use these namespaces
// without repeating the using statements

// Example.cs (no using statements needed)
namespace MyApp;

public class UserService
{
    public async Task<List<User>> GetUsersAsync()
    {
        // Can use List<T>, Task, etc. without local using statements
        return new List<User>();
    }
}

public record User(int Id, string Name);

Global static using

Use global static using to import static members from a type once for the entire project.

Valid since C# 10.0

// Usings.cs - define global using statements including static usings
global using System;
global using static System.Console;
global using static System.Math;

// Now WriteLine and static Math members are available everywhere

// Calculator.cs
namespace MyApp;

public class Calculator
{
    public double CalculateHypotenuse(double a, double b)
    {
        // Can use WriteLine and Sqrt without System or Math prefixes
        double result = Sqrt(Pow(a, 2) + Pow(b, 2));
        WriteLine($"Hypotenuse: {result}");
        return result;
    }
}

Learn more

Global Using Directives (Microsoft Learn)