How to use :: Namespace Alias Qualifier in C#

Namespace Alias Qualifier(::) makes the use of alias name in place of longer namespace and it provides a way to avoid ambiguous definitions of the classes. It is always positioned between two identifiers. The qualifier looks like two colons(::) with an alias name and the class name. It can be global. Thus it doesn’t invoke a lookup in the aliased namespace but in the global namespace.

Syntax:

alias_name::class-name;

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to use
// the :: Namespace Alias Qualifier
using System;
  
// creating aliased name
using first = firstnamespace;
using sec = secondnamespace;
  
namespace Geeks {
  
class GFG {
  
    // Main Method
    static void Main()
    {
  
        // use of Namespace alias qualifier(::)
        first::GFG1 obj1 = new first::GFG1(); 
        obj1.display();
    }
}
}
  
// Both namespaces have a 
// class named GFG1
namespace firstnamespace {
  
class GFG1 {
  
    public void display()
    {
        Console.WriteLine("It is the first namespace.");
    }
}
}
  
namespace secondnamespace {
  
class GFG1 {
  
    public void display()
    {
        Console.WriteLine("It is the second namespace.");
    }
}
}

chevron_right


Output:

It is the first namespace.

Note: The namespace alias qualifier :: is only used for the namespaces or aliases and cannot be used for subclasses.

Example:

System.Collections::lists obj= new System.Collections.lists() // illegal
aliasname = System.Collections;
aliasname::lists obj = new aliasname::lists(); // Legal
filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to use
// the :: Namespace Alias Qualifier
using aliasname = System.Collections;
  
namespace Geeks {
  
class GFG {
  
    // Main Method
    static void Main()
    {
        // using :: Namespace Alias Qualifier
        aliasname::Hashtable obj = new aliasname::Hashtable();
  
        // Add items to the table.
        obj.Add("ASCII value of A is:", "65");
        obj.Add("ASCII value of B is:", "66");
  
        // displaying the result
        foreach(string i in obj.Keys)
        {
            System.Console.WriteLine(i + " " + obj[i]);
        }
    }
}
}

chevron_right


Output:

ASCII value of A is: 65
ASCII value of B is: 66



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.