logo
CSharp_Prog_Guide

Псевдонимы пространств имен

Директива using также может использоваться для создания псевдонима пространства имен. Например, в случае использования написанного прежде пространства имен, содержащего вложенные пространства имен, можно объявить псевдоним для обеспечения быстрого способа обращения к одному из них, как показано в следующем примере:

using Co = Company.Proj.Nested; // define an alias to represent a namespace

Using Namespaces to control scope

The namespace keyword is used to declare a scope. The ability to create scopes within your project helps organize code and lets you create globally-unique types. In the following example, a class titled SampleClass is defined in two namespaces, one nested inside the other. The . Operator is used to differentiate which method gets called.

namespace SampleNamespace

{

class SampleClass

{

public void SampleMethod()

{

System.Console.WriteLine("SampleMethod inside SampleNamespace");

}

}

// Create a nested namespace, and define another class.

namespace NestedNamespace

{

class SampleClass

{

public void SampleMethod()

{

System.Console.WriteLine("SampleMethod inside NestedNamespace");

}

}

}

class Program

{

static void Main(string[] args)

{

// Displays "SampleMethod inside SampleNamespace."

SampleClass outer = new SampleClass();

outer.SampleMethod();

// Displays "SampleMethod inside SampleNamespace."

SampleNamespace.SampleClass outer2 = new SampleNamespace.SampleClass();

outer2.SampleMethod();

// Displays "SampleMethod inside NestedNamespace."

NestedNamespace.SampleClass inner = new NestedNamespace.SampleClass();

inner.SampleMethod();

}

}}