Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The global keyword

Reference for the C# global keyword: the global:: namespace alias that avoids name clashes, and global using directives that apply to every file in a project.

global appears in two places, and both are about namespaces. In global::Name, it is the alias of the root namespace, the one that contains every other namespace. In global using, it is a modifier that makes a using directive apply to every source file in the compilation, not just the file where you wrote it.

The alias solves name clashes: if you have your own class called System, plain System.Console would look inside your class. global::System.Console always starts at the top. The modifier solves repetition: put global using System.Text; once and drop the line from every file.

C#
global using System.Text;                 // applies to the whole project
var sb = new global::System.Text.StringBuilder();

Try it

C# global:: resolves name clashes

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.

C# global using directives

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

C#
global::Namespace.Type                // alias for the root namespace
global using Namespace;               // namespace
global using static Namespace.Type;   // static members
global using Alias = Namespace.Type;  // alias
Form Meaning
global:: Starts name lookup at the root namespace, bypassing any local or nested name with the same first segment.
global using A using directive that is applied to every file of the project. It must come before any non-global using directive and before any other declaration in the file.

Errors: a global using placed after a regular using or a type declaration gives CS8914. Duplicate global usings of the same namespace produce hidden diagnostic CS8933.

Usage notes

  • In a project, collect global usings in one file (often GlobalUsings.cs) or in the project file with <Using Include="System.Text" />. The .NET SDK also adds an implicit set per project type through <ImplicitUsings>enable</ImplicitUsings>. On this site's runner the implicit set is System, System.Collections.Generic, System.IO, System.Linq, System.Net.Http, System.Threading and System.Threading.Tasks.
  • Global usings have project-wide effect, so a using static or alias added in one file silently changes name lookup everywhere. Keep them to the namespaces you really use everywhere.
  • global:: is also how generated code (source generators, designers) protects itself from names in user code, which is why you see it all over generated files.
  • The types of the global namespace (those declared outside any namespace, such as classes in top-level-statement programs) are members of global::. You can refer to your own top-level Helper class as global::Helper from inside a namespace that declares another Helper.
  • Only one global:: form exists for namespaces. For assemblies, the related feature is extern alias.
  • global is a contextual keyword: it is only special in these two positions, so int global = 1; is legal, though confusing.