它通常放置在两个标识符之间,例如:

global::System.Console.WriteLine("Hello World");

这将调用全局命名空间中的查找,而不是在别名命名空间中。

 

那到底在什么情况下使用呢?

当我们自定义的命名空间与.NET Framework系统命名空间有冲突时,而我们又要调用系统命名空间中的类。

Console 类型。
using System;
class TestApp
{
    // Define a new class called 'System' to cause problems.
    public class System { }

    // Define a constant called 'Console' to cause more problems.
    const int Console = 7;
    const int number = 66;

    static void Main()
    {
        // The following line causes an error. It accesses TestApp.Console,
        // which is a constant.
        //Console.WriteLine(number);
    }
}

System.Console 仍然会导致错误:

System.Console.WriteLine(number);

global::System.Console 避免这一错误,如下所示:

global::System.Console.WriteLine(number);

 

在这种情况下,全局命名空间限定符可保证您可以指定根命名空间。

 

.Hashtable 的实例。

using colAlias = System.Collections; namespace System { class TestClass { static void Main() { // Searching the alias: colAlias::Hashtable test = new colAlias::Hashtable(); // Add items to the table. test.Add("A", "1"); test.Add("B", "2"); test.Add("C", "3"); foreach (string name in test.Keys) { // Searching the global namespace: global::System.Console.WriteLine(name + " " + test[name]); } } } }

相关文章:

  • 2021-06-26
  • 2021-12-08
  • 2021-09-04
  • 2022-12-23
  • 2021-10-25
  • 2022-12-23
  • 2021-05-20
  • 2022-12-23
猜你喜欢
  • 2021-07-05
  • 2022-12-23
  • 2022-03-06
  • 2021-12-28
  • 2021-07-15
  • 2021-11-10
相关资源
相似解决方案