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 :: operator, formally known as the namespace alias qualifier, explicitly instructs the C# compiler to resolve an identifier within a specific namespace alias. It overrides the default hierarchical scope resolution rules, guaranteeing that the compiler binds the identifier strictly to the aliased scope and preventing local types or namespaces from shadowing external ones.

Syntax

alias_name::identifier
  • alias_name: A namespace alias defined via a using directive, an extern alias directive, or the contextual keyword global.
  • identifier: The specific type or child namespace being accessed within the aliased scope.

Mechanics

Unlike the member access operator (.), which navigates down a namespace or object hierarchy based on proximity and standard resolution rules, the :: operator anchors the lookup exclusively to the specified alias. The left-hand side of the :: operator must always be a valid alias; it cannot be a standard namespace name or a type.

Custom Alias Resolution

When a custom alias is defined, the :: operator binds the subsequent identifier directly to the namespace mapped by that alias.
using IOAlias = System.IO;

namespace Application
{
    class Program
    {
        // The compiler looks for 'FileStream' strictly inside 'System.IO'
        IOAlias::FileStream stream; 
    }
}

The global Contextual Keyword

The global contextual keyword acts as a predefined alias for the unnamed global namespace of the entire compilation context. This encompasses all referenced assemblies and libraries (such as the .NET Base Class Library), not just the application’s own root namespace. When global:: is invoked, the compiler bypasses all nested or local scopes and begins its type lookup at the absolute top level of the compilation context. This is necessary when a type declared in a nested declaration space shadows a global namespace.
namespace MyApp
{
    // A local class inside MyApp that shadows the global System namespace
    class System 
    { 
        public class Console { }
    }

    class Program
    {
        static void Main()
        {
            // Resolves to the local class (MyApp.System.Console)
            System.Console localConsole = new System.Console(); 

            // Resolves to the .NET BCL System namespace
            global::System.Console.WriteLine("Global namespace accessed."); 
        }
    }
}

External Assembly Aliasing (extern alias)

The :: operator is the primary mechanism used in conjunction with the extern alias directive to resolve fully qualified type name collisions between different referenced assemblies. When two assemblies contain the exact same namespace and type, an external alias is assigned to one or both assemblies at the compiler level, and the :: operator routes the lookup to the specific assembly.
// Directives referencing the externally aliased assemblies
extern alias GridV1;
extern alias GridV2;

class Program
{
    static void Main()
    {
        // Resolves DataGrid from the assembly aliased as GridV1
        GridV1::Controls.DataGrid oldGrid = new GridV1::Controls.DataGrid();

        // Resolves DataGrid from the assembly aliased as GridV2
        GridV2::Controls.DataGrid newGrid = new GridV2::Controls.DataGrid();
    }
}

Compilation Rules

  1. Invalid Left-Hand Operand: If the token preceding :: is not a recognized using alias, extern alias, or global, the compiler emits error CS0432.
  2. Alias Hiding: If a local type shares a name with a namespace alias, the :: operator ensures the alias takes precedence, whereas the . operator would resolve to the local type.
Master C# with Deep Grasping Methodology!Learn More