C# 命名空間(Namespace)
命名空間的設計目的是提供一種讓一組名稱與其他名稱分隔開的方式。在一個命名空間中聲明的類的名稱與另一個命名空間中聲明的相同的類的名稱不衝突。
我們舉一個電腦系統中的例子,一個檔夾(目錄)中可以包含多個檔夾,每個檔夾中不能有相同的檔案名,但不同檔夾中的檔可以重名。
定義命名空間
命名空間的定義是以關鍵字 namespace 開始,後跟命名空間的名稱,如下所示:
namespace namespace_name
{
// 代碼聲明
}
{
// 代碼聲明
}
為了調用支持命名空間版本的函數或變數,會把命名空間的名稱置於前面,如下所示:
namespace_name.item_name;
下麵的程式演示了命名空間的用法:
實例
using System;
namespace first_space
{
class namespace_cl
{
public void func()
{
Console.WriteLine("Inside first_space");
}
}
}
namespace second_space
{
class namespace_cl
{
public void func()
{
Console.WriteLine("Inside second_space");
}
}
}
class TestClass
{
static void Main(string[] args)
{
first_space.namespace_cl fc = new first_space.namespace_cl();
second_space.namespace_cl sc = new second_space.namespace_cl();
fc.func();
sc.func();
Console.ReadKey();
}
}
namespace first_space
{
class namespace_cl
{
public void func()
{
Console.WriteLine("Inside first_space");
}
}
}
namespace second_space
{
class namespace_cl
{
public void func()
{
Console.WriteLine("Inside second_space");
}
}
}
class TestClass
{
static void Main(string[] args)
{
first_space.namespace_cl fc = new first_space.namespace_cl();
second_space.namespace_cl sc = new second_space.namespace_cl();
fc.func();
sc.func();
Console.ReadKey();
}
}
當上面的代碼被編譯和執行時,它會產生下列結果:
Inside first_space Inside second_space
using 關鍵字
using 關鍵字表明程式使用的是給定命名空間中的名稱。例如,我們在程式中使用 System 命名空間,其中定義了類 Console。我們可以只寫:
Console.WriteLine ("Hello there");
我們可以寫完全限定名稱,如下:
System.Console.WriteLine("Hello there");
您也可以使用 using 命名空間指令,這樣在使用的時候就不用在前面加上命名空間名稱。該指令告訴編譯器隨後的代碼使用了指定命名空間中的名稱。下麵的代碼演示了命名空間的應用。
讓我們使用 using 指定重寫上面的實例:
實例
using System;
using first_space;
using second_space;
namespace first_space
{
class abc
{
public void func()
{
Console.WriteLine("Inside first_space");
}
}
}
namespace second_space
{
class efg
{
public void func()
{
Console.WriteLine("Inside second_space");
}
}
}
class TestClass
{
static void Main(string[] args)
{
abc fc = new abc();
efg sc = new efg();
fc.func();
sc.func();
Console.ReadKey();
}
}
using first_space;
using second_space;
namespace first_space
{
class abc
{
public void func()
{
Console.WriteLine("Inside first_space");
}
}
}
namespace second_space
{
class efg
{
public void func()
{
Console.WriteLine("Inside second_space");
}
}
}
class TestClass
{
static void Main(string[] args)
{
abc fc = new abc();
efg sc = new efg();
fc.func();
sc.func();
Console.ReadKey();
}
}
當上面的代碼被編譯和執行時,它會產生下列結果:
Inside first_space Inside second_space
嵌套命名空間
命名空間可以被嵌套,即您可以在一個命名空間內定義另一個命名空間,如下所示:
namespace namespace_name1 { // 代碼聲明 namespace namespace_name2 { // 代碼聲明 } }
您可以使用點(.)運算符訪問嵌套的命名空間的成員,如下所示:
實例
using System;
using SomeNameSpace;
using SomeNameSpace.Nested;
namespace SomeNameSpace
{
public class MyClass
{
static void Main()
{
Console.WriteLine("In SomeNameSpace");
Nested.NestedNameSpaceClass.SayHello();
}
}
// 內嵌命名空間
namespace Nested
{
public class NestedNameSpaceClass
{
public static void SayHello()
{
Console.WriteLine("In Nested");
}
}
}
}
using SomeNameSpace;
using SomeNameSpace.Nested;
namespace SomeNameSpace
{
public class MyClass
{
static void Main()
{
Console.WriteLine("In SomeNameSpace");
Nested.NestedNameSpaceClass.SayHello();
}
}
// 內嵌命名空間
namespace Nested
{
public class NestedNameSpaceClass
{
public static void SayHello()
{
Console.WriteLine("In Nested");
}
}
}
}
當上面的代碼被編譯和執行時,它會產生下列結果:
In SomeNameSpace In Nested