Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The global using directive, introduced in C# 10, instructs the compiler to apply a namespace import, static import, or alias across all source files within an entire compilation unit. By defining a dependency globally, the specified namespace or type becomes implicitly available to every .cs file in the project during the compilation process.

Syntax Variations

The global modifier can be applied to all three standard forms of the using directive:
// 1. Standard namespace import
global using System.Collections.Generic;

// 2. Static import (imports static members of a specific type)
global using static System.Console;

// 3. Aliasing (creates a global alias for a namespace or type)
global using JsonDict = System.Collections.Generic.Dictionary<string, System.Text.Json.JsonElement>;

Lexical Placement and Rules

To ensure successful compilation, global using directives are subject to strict ordering rules within the source file they inhabit:
  1. Top-Level Precedence: A global using directive must precede all non-global using directives, namespace declarations, and type definitions within the file.
  2. File Independence: The directive can be placed in any .cs file within the project. The compiler aggregates all global usings across the compilation unit before resolving types.
  3. Redundancy Handling: If a namespace is imported globally, declaring a standard using directive for the same namespace in an individual file is syntactically valid but redundant; the compiler will safely ignore the duplicate.
Valid Ordering:
global using System.Text;
global using static System.Math;

using System.IO; // Non-global must come after global

namespace ApplicationCore;
// Type definitions follow...
Invalid Ordering (Compilation Error CS8915):
using System.IO; 

global using System.Text; // Error: global using must precede non-global using

MSBuild Integration (Implicit Usings)

The .NET SDK leverages the global using feature at the build level via the <ImplicitUsings> property in the .csproj file.
<PropertyGroup>
  <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
When enabled, the MSBuild process automatically generates a file named <ProjectName>.GlobalUsings.g.cs in the obj/Debug/netX.0/ directory. This auto-generated file contains a predefined set of global using directives based on the project’s SDK type (e.g., Microsoft.NET.Sdk vs Microsoft.NET.Sdk.Web), injecting foundational namespaces into the compilation unit without requiring manual source code declarations.
Master C# with Deep Grasping Methodology!Learn More