命名空间Alias Qualifier( : :)使用别名来代替更长的命名空间,并且它提供了一种避免类的模棱两可的方法。它始终位于两个标识符之间。限定符看起来像两个冒号(::),分别带有别名和类名。它可以是全球性的。因此,它不会在别名命名空间中而是在全局命名空间中调用查找。
句法:
alias_name::class-name;
例子:
// 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.");
}
}
}
输出:
It is the first namespace.
注意:名称空间别名qualifier ::仅用于名称空间或别名,不能用于子类。
例子:
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]);
}
}
}
}
输出:
ASCII value of A is: 65
ASCII value of B is: 66