Skip to main content
The using alias directive creates a user-defined identifier for an existing namespace or type. It instructs the C# compiler to substitute the alias with the target namespace or type during the compilation process. This substitution is strictly a compile-time mechanism; aliases do not create new types and do not exist in the emitted Intermediate Language (IL).

Syntax

Scope and Modifiers

  • Compilation Unit Scope: When declared at the top of a file, preceding any namespace or type declarations, the alias is scoped to the entire compilation unit (the specific source file).
  • Namespace Scope: When declared inside a block-scoped namespace { ... } declaration, the alias is scoped only to that specific namespace body. It must be placed at the top of the namespace body, before any type or nested namespace declarations.
  • Global Scope (C# 10+): Applying the global modifier extends the alias to the entire compilation (all compilation units within the project or assembly). Global aliases must precede any non-global using directives.

Resolution Rules

The right-hand side of the alias assignment is resolved relative to the declaration space containing the using directive. It is resolved as if the containing file or namespace had no other using directives to assist in resolution. It does not need to be fully qualified from the root namespace, but it cannot implicitly rely on imported namespaces.

Supported Alias Targets

1. Namespace Aliasing Maps an identifier to a specific namespace hierarchy.
2. Constructed Type Aliasing Maps an identifier to a specific, fully constructed type. Open generics (e.g., List<>) cannot be aliased.
3. Any Type Aliasing (C# 12+) Starting in C# 12, the directive supports aliasing virtually any valid C# type, including tuples, arrays, pointer types, and function pointers.

The Namespace Alias Qualifier (::)

When an alias shares a name with a locally declared type or namespace, the compiler may encounter an ambiguity error. The namespace alias qualifier operator (::) forces the compiler to resolve the identifier specifically against the defined alias rather than the local scope.
Tired of Poor C# Skills? Fix That With Deep Grasping!Learn More