C# 命名空间
命名空间的设计目的是提供一种让一组名称与其他名称分隔开的方式。在一个命名空间中声明的类的名称与另一个命名空间中声明的相同的类的名称不冲突。
一、定义命名空间
命名空间的定义是以关键字 namespace 开始,后跟命名空间的名称,如下所示:
1 2 3 4 5 |
<span class="hljs-keyword">namespace</span> <span class="hljs-title">namespace_name</span> { <span class="hljs-comment">// 代码声明</span> } |
为了调用支持命名空间版本的函数或变量,会把命名空间的名称置于前面,如下所示:
1 2 |
namespace_name.item_name; |
下面的程序演示了命名空间的用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<span class="hljs-keyword">using</span> System; <span class="hljs-keyword">namespace</span> <span class="hljs-title">first_space</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">namespace_cl</span> { <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">func</span>() </span>{ Console.WriteLine(<span class="hljs-string">"Inside first_space"</span>); } } } <span class="hljs-keyword">namespace</span> <span class="hljs-title">second_space</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">namespace_cl</span> { <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">func</span>() </span>{ Console.WriteLine(<span class="hljs-string">"Inside second_space"</span>); } } } <span class="hljs-keyword">class</span> <span class="hljs-title">TestClass</span> { <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>[] args</span>) </span>{ first_space.namespace_cl fc = <span class="hljs-keyword">new</span> first_space.namespace_cl(); second_space.namespace_cl sc = <span class="hljs-keyword">new</span> second_space.namespace_cl(); fc.func(); sc.func(); Console.ReadKey(); } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 2 3 |
Inside first_space Inside second_space |
二、using 关键字
using 关键字表明程序使用的是给定命名空间中的名称。例如,我们在程序中使用 System 命名空间,其中定义了类 Console。我们可以只写:
1 2 |
Console.WriteLine (<span class="hljs-string">"Hello there"</span>); |
我们可以写完全限定名称,如下:
1 2 |
System.Console.WriteLine(<span class="hljs-string">"Hello there"</span>); |
您也可以使用 using 命名空间指令,这样在使用的时候就不用在前面加上命名空间名称。该指令告诉编译器随后的代码使用了指定命名空间中的名称。下面的代码演示了命名空间的应用。
让我们使用 using 指定重写上面的实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<span class="hljs-keyword">using</span> System; <span class="hljs-keyword">using</span> first_space; <span class="hljs-keyword">using</span> second_space; <span class="hljs-keyword">namespace</span> <span class="hljs-title">first_space</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">abc</span> { <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">func</span>() </span>{ Console.WriteLine(<span class="hljs-string">"Inside first_space"</span>); } } } <span class="hljs-keyword">namespace</span> <span class="hljs-title">second_space</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">efg</span> { <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">func</span>() </span>{ Console.WriteLine(<span class="hljs-string">"Inside second_space"</span>); } } } <span class="hljs-keyword">class</span> <span class="hljs-title">TestClass</span> { <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>[] args</span>) </span>{ abc fc = <span class="hljs-keyword">new</span> abc(); efg sc = <span class="hljs-keyword">new</span> efg(); fc.func(); sc.func(); Console.ReadKey(); } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 2 3 |
Inside first_space Inside second_space |
三、嵌套命名空间
命名空间可以被嵌套,即您可以在一个命名空间内定义另一个命名空间,如下所示:
1 2 3 4 5 6 7 8 9 |
<span class="hljs-keyword">namespace</span> <span class="hljs-title">namespace_name1</span> { <span class="hljs-comment">// 代码声明</span> <span class="hljs-keyword">namespace</span> <span class="hljs-title">namespace_name2</span> { <span class="hljs-comment">// 代码声明</span> } } |
您可以使用点(.)运算符访问嵌套的命名空间的成员,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<span class="hljs-keyword">using</span> System; <span class="hljs-keyword">using</span> SomeNameSpace; <span class="hljs-keyword">using</span> SomeNameSpace.Nested; <span class="hljs-keyword">namespace</span> <span class="hljs-title">SomeNameSpace</span> { <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span> { <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>() </span>{ Console.WriteLine(<span class="hljs-string">"In SomeNameSpace"</span>); Nested.NestedNameSpaceClass.SayHello(); } } <span class="hljs-comment">// 内嵌命名空间</span> <span class="hljs-keyword">namespace</span> <span class="hljs-title">Nested</span> { <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">NestedNameSpaceClass</span> { <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">SayHello</span>() </span>{ Console.WriteLine(<span class="hljs-string">"In Nested"</span>); } } } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 2 |
In SomeNameSpace In Nested |