logo
CSharp_Prog_Guide

Полные имена

Пространства имен и типы имеют уникальные названия, описываемые полными именами, показывающими логическую иерархию. Например, инструкция A.B подразумевает, что A — это имя пространства имен или типа, а B — это вложенный в него тип.

В следующем примере показаны вложенные классы и пространства имен. Полное имя указано в качестве примечания после каждой сущности.

---

В предыдущем фрагменте кода:

Используя предыдущий фрагмента кода, можно добавить новый член: класс C3, в пространство имен N1.N2, как показано ниже:

namespace N1.N2

{

class C3 // N1.N2.C3

{

}

}

In general, use :: to reference a namespace alias or global:: to reference the global namespace and . to qualify types or members.

It is an error to use :: with an alias that references a type instead of a namespace. For example:

using Alias = System.Console;

class TestClass

{

static void Main()

{

// Error

//Alias::WriteLine("Hi");

// OK

Alias.WriteLine("Hi");

}

}

Remember that the word global is not a predefined alias; therefore, global.X does not have any special meaning. It acquires a special meaning only when it is used with ::.

A warning (see Compiler Warning (level 2) CS0440) is generated if you define an alias named global because global:: always references the global namespace and not an alias. For example, the following line generates the warning:

using global = System.Collections; // Warning

Using :: with aliases is a good idea and protects against the unexpected introduction of additional types. For example, consider this example:

using Alias = System;

namespace Library

{

public class C : Alias.Exception { }

}

This works, but if a type named Alias were to subsequently be introduced, Alias. would bind to that type instead. Using Alias::Exception insures that Alias is treated as a namespace alias and not mistaken for a type.

В общем, используйте ключевое слово :: для обращения к псевдониму пространства имен или ключевое слово global:: для обращения к глобальному пространству имен и ключевое слово . для уточнения типов или членов.

Ошибкой является использование ключевого слова :: с псевдонимом, ссылающимся на тип, а не на пространство имен. Например:

---

Обратите внимание, что ключевое слово global не является предопределенным псевдонимом. Следовательно, имя global.X не имеет какого-либо специального значения. Оно приобретает специальное значение только при использовании с ::.

В случае определения псевдонима global генерируется предупреждение (см. раздел Предупреждение компилятора (уровень 2) CS0440), потому что ключевое слово global:: всегда ссылается на глобальное пространство имен, а не на псевдоним. Например, следующая строка приведет к генерированию предупреждения:

using global = System.Collections; // Warning

Использование ключевого слова :: с псевдонимами является правильной методикой, защищающей от неожиданного введения дополнительных типов. Рассмотрим, например, следующий фрагмент кода:

using Alias = System;

namespace Library

{

public class C : Alias.Exception { }

}

Этот код работает, но если в последующем будет введен тип Alias, то конструкция Alias. станет связана с этим типом. Использование конструкции Alias::Exception гарантирует, что имя Alias будет обрабатываться как псевдоним пространства имен и не будет ошибочно принято за тип.

How to: Use the Namespace Alias Qualifier

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.

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()

{

// Error Accesses TestApp.Console

//Console.WriteLine(number);

}

}

Using System.Console still results in an error because the System namespace is hidden by the class TestApp.System:

// Error Accesses TestApp.System

System.Console.WriteLine(number);

However, you can work around this error by using global::System.Console, like this:

// OK

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

When the left identifier is global, the search for the right identifier starts at the global namespace. For example, the following declaration is referencing TestApp as a member of the global space.

class TestClass : global::TestApp

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.