A Guide to C# 10 Global Usings
An introduction to the new global using directives feature in C# 10. Learn how this feature can help you reduce repetitive using statements and clean up the top of your C# files.
One of the goals of recent C# releases has been to reduce the amount of boilerplate and ceremony required to write code. C# 9 introduced top-level statements, and C# 10, released with .NET 6, continues this trend with a new feature: global using directives.
This feature is designed to tackle a common source of clutter in C# files: the long list of repetitive using
statements at the top of every file.
The Problem: Repetitive using
Statements
In any reasonably sized C# project, you'll find that many of your files start with the exact same set of using
directives.
// File1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MyWebApp.Models;
// ... code ...
// File2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MyWebApp.Models;
// ... more code ...
This is redundant. These common namespaces are used throughout your project, and having to declare them in every single file adds unnecessary clutter.
The Solution: global using
C# 10 allows you to declare a using
directive as global
. A global using directive applies to all source files in the compilation (i.e., your entire project).
You can put these global using statements in any .cs
file, but the standard practice is to create a single, dedicated file for them, often named GlobalUsings.cs
.
GlobalUsings.cs
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks;
global using MyWebApp.Models;
Now, you can remove these five using
statements from all other files in your project. The namespaces will be automatically available everywhere, resulting in much cleaner and more focused code files.
Implicit Usings in .NET 6
To make this even easier, the new .NET 6 project templates take this a step further with a feature called implicit usings. If you create a new .NET 6 project, your .csproj
file will contain this line by default:
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
When this feature is enabled, the .NET SDK automatically generates a set of global using directives for the most common namespaces related to your project type. For example, for an ASP.NET Core project, it will automatically generate global usings for namespaces like System.Net.Http
, Microsoft.AspNetCore.Builder
, Microsoft.Extensions.Logging
, and more.
This means that for many common scenarios, you won't have to write any using
statements at all!
The specific set of implicitly included namespaces is determined by which SDK your project uses (e.g., Microsoft.NET.Sdk
vs. Microsoft.NET.Sdk.Web
).
Where to Use Global Usings
- Implicit Usings: You should generally leave the
ImplicitUsings
feature enabled in your .NET 6+ projects, as it provides a great set of defaults. - Explicit
global using
: You should create aGlobalUsings.cs
file for any other namespaces that are commonly used across your entire project. This is perfect for your application's own core namespaces (e.g.,MyWebApp.Services
,MyWebApp.Data
).
This combination allows you to dramatically reduce the number of using
statements you need to write manually.
Conclusion
Global using directives are a simple but powerful feature for reducing boilerplate and cleaning up your C# code. By centralizing your common namespace imports, you can make your code files cleaner and more focused on their actual logic. Combined with the new implicit usings feature in the .NET 6 SDK, it represents a significant quality-of-life improvement for all C# developers.