How to use :: Namespace Alias Qualifier in C#
Last Updated :
06 Mar, 2019
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:
using System;
using first = firstnamespace;
using sec = secondnamespace;
namespace Geeks {
class GFG {
static void Main()
{
first::GFG1 obj1 = new first::GFG1();
obj1.display();
}
}
}
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
using aliasname = System.Collections;
namespace Geeks {
class GFG {
static void Main()
{
aliasname::Hashtable obj = new aliasname::Hashtable();
obj.Add( "ASCII value of A is:" , "65" );
obj.Add( "ASCII value of B is:" , "66" );
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
Share your thoughts in the comments
Please Login to comment...