• To reduce on boilerplate code, Microsoft has introduced Global using directives
  • When specified as Global, a directive becomes available to the whole project.
  • Best practice would be to create a dedicated class GlobalUsings.cs and specify the directives as below. It also works with static and aliases:
1
2
3
global using Mycustom.namespace;
global using static Mystatic.namespace;
global using Mine = MyOther.namespace;
  • Global directives can also be defined in the project file, a feature called Implicit Usings and enabled with flag:
1
<ImplicitUsings>enable</ImplicitUsings>  
  • With Implicit Usings, a global using file get generated in the obj folder, ending with “.GlobalUsings.g.cs”
  • Depending on the project type, the compiler will add a set of global directives to the generated global usings eg for a console application:
1
2
3
4
5
6
7
System
System.Collections.Generic
System.IO
System.Linq
System.Net.Http
System.Threading
System.Threading.Tasks
  • There is also the flexibility of adding or removing from the default list:
1
2
3
4
<ItemGroup>
  <Using Remove="System.Threading" />
  <Using Include="Microsoft.Extensions.Logging" />
</ItemGroup>