如何:使用全局命名空间别名(C# 编程指南)How to: Use the Global Namespace Alias (C# Programming Guide)

当具有同一名称的其他实体可能隐藏了成员时,访问全局命名空间中的成员的功能将十分有用。The ability to access a member in the global namespace is useful when the member might be hidden by another entity of the same name.

例如,在下面的代码中,Console 解析为 TestApp.Console 而不是 System 命名空间中的 Console 类型。For example, in the following code, Console resolves to TestApp.Console instead of to the Console type in the System namespace.

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 仍会导致错误,因为类 TestApp.System 隐藏了 System 命名空间:Using System.Console still results in an error because the System namespace is hidden by the class TestApp.System:

// The following line causes an error. It accesses TestApp.System,
// which does not have a Console.WriteLine method.
System.Console.WriteLine(number);

但是,可以使用 global::System.Console 解决此错误,如下所示:However, you can work around this error by using global::System.Console, like this:

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

如果左标识符为 global,则会在全局命名空间开始搜索右标识符。When the left identifier is global, the search for the right identifier starts at the global namespace. 例如,以下声明引用 TestApp 作为全局空间的成员。For example, the following declaration is referencing TestApp as a member of the global space.

class TestClass : global::TestApp

显然,不建议将自己的命名空间的名称创建为 System,并且不可能会遇到发生此情况的代码。Obviously, creating your own namespaces called System is not recommended, and it is unlikely you will encounter any code in which this has happened. 但是,在大型项目中,很有可能会以一种或另一种形式发生命名空间重复。However, in larger projects, it is a very real possibility that namespace duplication may occur in one form or another. 在这些情况下,全局命名空间限定符可保证指定根命名空间。In these situations, the global namespace qualifier is your guarantee that you can specify the root namespace.

示例Example

在此示例中,命名空间 System 用于包括类 TestClass,因此, global::System.Console 必须用于引用 System.Console 类,该类由 System 命名空间隐藏。In this example, the namespace System is used to include the class TestClass therefore, global::System.Console must be used to reference the System.Console class, which is hidden by the System namespace. 此外,别名 colAlias 用于引用命名空间 System.Collections;因此,System.Collections.Hashtable 实例是使用此别名而不是命名空间创建的。Also, the alias colAlias is used to refer to the namespace System.Collections; therefore, the instance of a System.Collections.Hashtable was created using this alias instead of the namespace.

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

A 1 B 2 C 3A 1 B 2 C 3

请参阅See also