liveBook · Manning

Global using directive

As explained in .NET in Action, Second Edition

A global using directive makes a namespace or type import available throughout all files in a C# compilation unit.

A global using directive makes a namespace or type import available throughout every file in a C# project, reducing repetitive using declarations. The compiler can also supply common .NET namespaces through enabled implicit usings. section 2.5

Use global usings when a namespace appears across many classes:

  • Place declarations such as global using Figgle; in a dedicated GlobalUsings.cs file. section 2.5
  • Declare imports in the project file with an ItemGroup and Using Include entry. section 2.5
  • Keep ordinary global usings distinct from global static usings, which expose static members without their containing type name. section 2.6
Listing 2.10 Global using in Project file
<Project Sdk="Microsoft.NET.Sdk">
  <!-- ... -->
  <ItemGroup>
    <Using Include="Figgle" />    #1
  </ItemGroup>
</Project>

The project-file form shows how a namespace becomes globally available without adding declarations to each source file. section 2.5

How .NET in Action, Second Edition covers it

How deep it goes
Explains source-file and project-file declarations, but does not descend into compiler implementation.
Examples
Uses System, Figgle, GlobalUsings.cs, and project-file Using entries as concrete examples.
What it assumes
Assumes familiarity with C# namespaces, source files, and .csproj project configuration.
What you can do after
Apply it via C# declarations or .csproj entries to shorten source files.
How it connects
Connects global usings with implicit usings and static using statements.

Also explained in

Compare how 2 books explain global using directive →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.