C# Reference ยท .NET, Tools and Concepts
Implicit usings and global usings
Reference for implicit usings in .NET SDK projects: which namespaces are imported by default, global using directives, customizing with Using items and aliases.
Implicit usings are a feature of the .NET SDK, not of the C# language. When the project file contains <ImplicitUsings>enable</ImplicitUsings>, the SDK generates a file (named like MyApp.GlobalUsings.g.cs in the obj folder) that holds a list of global using directives. Every source file in the project then behaves as if it started with those using lines, so you can write List<int>, File.ReadAllText or Task.Delay without importing anything.
The language feature behind it is the global using directive (C# 10): global using System.Text; in any one file imports that namespace in every file of the compilation. You can write your own, with or without the SDK feature.
<PropertyGroup> <TargetFramework>net10.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup>
Try it
This site's runner compiles your program with the default console set already imported, so List<T>, LINQ, File, HttpClient and Task work without any using line.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
Namespaces imported by default
SDK (Sdk attribute) |
Namespaces added by ImplicitUsings |
|---|---|
Microsoft.NET.Sdk (console, libraries) |
System, System.Collections.Generic, System.IO, System.Linq, System.Net.Http, System.Threading, System.Threading.Tasks |
Microsoft.NET.Sdk.Web |
The list above plus System.Net.Http.Json, Microsoft.AspNetCore.Builder, Microsoft.AspNetCore.Hosting, Microsoft.AspNetCore.Http, Microsoft.AspNetCore.Routing, Microsoft.Extensions.Configuration, Microsoft.Extensions.DependencyInjection, Microsoft.Extensions.Hosting, Microsoft.Extensions.Logging |
Microsoft.NET.Sdk.Worker |
The base list plus Microsoft.Extensions.Configuration, Microsoft.Extensions.DependencyInjection, Microsoft.Extensions.Hosting, Microsoft.Extensions.Logging |
The runner on this site uses the base list. Web examples here add their Microsoft.AspNetCore.* directives by hand because there is no Web SDK.
Changing the list
<ItemGroup> <Using Include="System.Text" /> <!-- add --> <Using Remove="System.Net.Http" /> <!-- remove --> <Using Include="System.Math" Static="true" /> <!-- global using static --> <Using Include="System.Text.StringBuilder" Alias="SB" /> <!-- global using SB = ... --> </ItemGroup>
Global using directive forms
| Form | Meaning |
|---|---|
global using System.Text; |
Imports a namespace in every file |
global using static System.Math; |
Imports the static members of a type everywhere |
global using Pair = (string, int); |
Defines an alias everywhere |
Global directives must come before any non-global using directive and before any declaration in their file, and a project often keeps all of them in one file such as GlobalUsings.cs.
Usage notes
- Libraries should be careful. Implicit usings only affect your own source, never the consumers, but a changed set can introduce ambiguities. Two types with the same name from two imported namespaces produce error CS0104 ("ambiguous reference"); qualify the name or add an alias.
- Existing
using System;lines are harmless. A redundant using is reported as an unnecessary directive (IDE0005), not as an error. - Missing namespaces give CS0246. For example,
StringBuilderwithoutusing System.Text;fails with "The type or namespace name 'StringBuilder' could not be found". - Older projects (SDK-style with
net5.0or earlier, or non-SDK.NET Frameworkprojects) do not have the feature; it needs the SDK from .NET 6 and C# 10. - Tooling shows nothing. The generated file lives in
obj/. Look at it when you wonder where a type comes from, or use "Go to definition". - Global usings are per compilation, so a test project does not inherit the main project's global usings; it defines its own.
global using staticofSystem.Mathmakes calls such asMax(a, b)short, but can hide where a method comes from. Use it sparingly.- Nullable context is separate.
<Nullable>enable</Nullable>is another property, see Nullable context.