How do global using directives work, and how do you declare one for an entire project?
Short answer
Prefixing a 'using' directive with 'global' (either directly in a .cs file, or conventionally in a dedicated GlobalUsings.cs file) makes that namespace available to EVERY file in the project automatically, without needing to repeat the same common 'using' statements (like…
Advertisement
AModel answer
Prefixing a 'using' directive with 'global' (either directly in a .cs file, or conventionally in a dedicated GlobalUsings.cs file) makes that namespace available to EVERY file in the project automatically, without needing to repeat the same common 'using' statements (like System, System.Linq) at the top of every single file.
// GlobalUsings.cs
global using System;
global using System.Linq;
global using System.Collections.Generic;
// Any other file in the project can now use List<T>, LINQ, etc. WITHOUT its own 'using' statementsReal-world example
Eliminating repetitive boilerplate 'using System; using System.Linq;' lines across every file in a large project.
Likely follow-ups
- Does the SDK automatically generate any implicit global usings for common namespaces, even without an explicit GlobalUsings.cs file?
Related topics