Open In App

How to use :: Namespace Alias Qualifier in C#

Last Updated : 06 Mar, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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.");
    }
}
}


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




// 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]);
        }
    }
}
}


Output:

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads