C#名称空间

本节课将介绍C#的名称空间。其目的是:
1.了解什么是名称空间。

2.了解如何实现"using"指示符。

3.了解"alias" 指示符的用法。

4.了解名称空间的成员的内容。

在第一课中,你已经在简单的hello程序中看到了"using System;"指示符的使用。该指示符可以让你使用System名称空间中的成员。在第一课中,未及对此作出详细介绍,现在我们来解释一下名称空间的具体用法。一旦学完了本节课,你将了解"using"指示符及其相关内容。

作为C#的元素,名称空间可以用来帮助组织程序的结构,可以避免两套代码集中命名的冲突。在程序代码中,使用名称空间是个良好的编程习惯,因为这有助于重用你的程序代码。

1.清单6-1. The C# Station Namespace: NamespaceCSS.cs
1 // Namespace Declaration
2 using System;
3 // The C# Station Namespace
4 namespace csharp_station
5 {
6//Program start class
7classNamespaceCSS
8{
9
10//Main begins program execution.
11publicstaticvoidMain()
12{
13//Write to console
14Console.WriteLine("This is the new C# Station Namespace.");
15}

16}

17}


说明

清单6-1演示了如何创建一个名称空间。把单词"namespace"放在"csharp_station"之前,就创建了一个名称空间。"csharp_station"名称空间内的大括号中包含了成员。

2.清单6-2 Nested Namespace 1: NestedNamespace1.cs
1 // Namespace Declaration
2 using System;
3 // The C# Station Tutorial Namespace
4 namespace csharp_station
5 {
6namespacetutorial
7{
8//Program start class
9classNamespaceCSS
10{
11//Main begins program execution.
12publicstaticvoidMain()
13{
14//Write to console
15Console.WriteLine("This is the new C# Station Tutorial Namespace.");
16}

17}

18}

19}


说明

名称空间可以建立一个代码的组织结构。一个良好的编程习惯是:用层次模式来组织你的名称空间。你可以把通用一些的名称放在最顶层,里层则放置一些专用一些的名称。这个层次系统可以用嵌套的名称空间表示。清单6-2演示了如何建立一个嵌套的名称空间。在不同的子名称空间内放置代码,从而组织好你的代码的结构。

3.清单6-3. Nested Namespace 2: NestedNamespace2.cs
1 // Namespace Declaration
2 using System;
3 // The C# Station Tutorial Namespace
4 namespace csharp_station.tutorial
5 {
6//Program start class
7classNamespaceCSS
8{
9//Main begins program execution.
10publicstaticvoidMain()
11{
12//Write to console
13Console.WriteLine("This is the new C# Station Tutorial Namespace.");
14}

15}

16}


说明


清单6-3演示了另外一种编写嵌套的名称空间的方法。在"csharp_station"和"tutorial"之间置入点运算符,表明这是嵌套的名称空间。结果同清单6-2。 相比而言,清单6-3 更易书写

4.清单6-4. Calling Namespace Members: NamespaceCall.cs
1 // Namespace Declaration
2 using System;
3 namespace csharp_station
4 {
5//nested namespace
6namespacetutorial
7{
8classmyExample1
9{
10publicstaticvoidmyPrint1()
11{
12Console.WriteLine("First Example of calling another namespace member.");
13}

14}

15}

16//Program start class
17classNamespaceCalling
18{
19//Main begins program execution.
20publicstaticvoidMain()
21{
22//Write to console
23tutorial.myExample1.myPrint1();
24csharp_station.tutorial.myExample2.myPrint2();
25}

26}

27}

28
29 // same namespace as nested namespace above
30 namespace csharp_station.tutorial
31 {
32classmyExample2
33{
34publicstaticvoidmyPrint2()
35{
36Console.WriteLine("Second Example of calling another namespace member.");
37}

38}

39}

40


说明

1.清单6-4 的例子演示了用完整的名称指示,调用名称空间的成员。

一个完整的名称指示包括名称空间名,以及调用的方法名。程序的上半部分,在"csharp-station"名称空间内嵌套的名称空间"tutorial"中,定义了类"myExample1"及其方法"myPrint1"。 Main()方法中用完整的名称指示:"tutorial.myExample1.myPrint()" 来进行调用。 因为Main()方法和tutorial名称空间位于同一名称空间内,如果使用"csharp_station"的全称不是必需的。

2.清单6-4的下半部分,也是名称空间"csharp_station.tutorial"的一部分。

类"myExample1"和"myExample2"都属于该名称空间。另外,也可以把它们分别写入不同的文件,同时它们仍然属于同一名称空间。在Main()方法中,调用"myPrint2"方法时,采用了全称:"csharp_station.tutorial.myExample2.myPrint2()"。 在这里,必须使用全称中"csharp_station",因为"myExample2"定义在外部。

3.注意:这里对两个不同的类起了不同的名字:

"myExample1"和"myExample2"这是因为对于每个名称空间来说,其中的成员必须有唯一的名称。 记住:它们都处于同一名称空间中,不能取名相同。方法"myPrint1"和"myPrint2" 名称的不同仅仅是为了方便起见,即使同名也没有问题,因为它们属于不同的类。

5.清单6-5. The using Directive: UsingDirective.cs
1 // Namespace Declaration
2 using System;
3 using csharp_station.tutorial;
4 // Program start class
5 class UsingDirective
6 {
7//Main begins program execution.
8publicstaticvoidMain()
9{
10//Call namespace member
11myExample.myPrint();
12}

13}

14
15 // C# Station Tutorial Namespace
16 namespace csharp_station.tutorial
17 {
18classmyExample
19{
20publicstaticvoidmyPrint()
21{
22Console.WriteLine("Example of using a using directive.");
23}

24}

25}

26

说明

调用方法时,如果你不想打入全称,可使用"using"指示符。在清单6-5中,有两个"using"指示符。第一个指示符是"using System",同本教程其它地方出现的"using"指示符相同。你不需要每次都打上"System",只需要打入该名称空间的成员方法名即可。在myPrint()中,"Console"是个"System"名称空间中的成员类,该类有个"WriteLine"的方法。该方法的全称是: "System.Console.WriteLine(...)"。

类似地,using指示符"using csharp_station.tutorial"可以让我们在使用 "csharp_station.tutorial" 名称空间的成员时,无需打入全称。所以,我们可以打入"myExample.myPrint()"。如果不使用"using"指示符,每次实现该方法时,我们就得打入"csharp_station.tutorial.myExample.myPrint()" 。

6.清单6-6. The Alias Directive: AliasDirective.cs
1 // Namespace Declaration
2 using System;
3 using csTut = csharp_station.tutorial.myExample; // alias
4 // Program start class
5 class AliasDirective
6 {
7//Main begins program execution.
8publicstaticvoidMain()
9{
10//Call namespace member
11csTut.myPrint();
12myPrint();
13}

14//Potentially ambiguous method.
15staticvoidmyPrint()
16{
17Console.WriteLine("Not a member of csharp_station.tutorial.myExample.");
18}

19}

20
21 // C# Station Tutorial Namespace
22 namespace csharp_station.tutorial
23 {
24classmyExample
25{
26publicstaticvoidmyPrint()
27{
28Console.WriteLine("This is a member of csharp_station.tutorial.myExample.");
29}

30}

31}

32


说明

1.有时,往往遇到取名较长的名称空间,而你可以把该名称变短些。

这样就增强了可读性,还避免了同名的冲突。清单6-6 演示了如何使用别名指示符,创建别名的格式例子是:"using csTut = csharp_station.tutorial.myExample"。表达式"csTut"可以取代"csharp_station.tutorial.myExample",用在本文件的任何地方。在Main()方法中就使用了"csTut"。

2.在Main()方法中,调用了"AliasDirective" 类中"myPrint" 方法。

这与"myExample" 类的"myPrint"方法同名。 虽然同名,这两个方法都各自正确地进行了调用,原因是:"myExample"类的"myPrint"方法用别名"csTut"表示。编译器能够准确地了解所要执行的是哪个方法。一旦漏掉了"csTut",编译器将两次调用"AliasDirective"类的"myPrint"方法。

3.另外一方面,如果我们没有创建别名指示符,而是添加了"using csharp_station.tutorial.myExample"之后,再调用myPrint(),编译器就会生成出错信息,因为它不知道究竟是调用. "csharp_station.tutorial.myExample.myPrint()"方法呢?还是去调用"AliasDirective.myPrint()"方法。所以使用名称空间是良好的编程习惯,可避免代码中的冲突现象。

小结
到现在为止,我们已经了解在名称空间中可以使用类,实际上,名称空间可以使用如下类型的数据:

类;结构;接口;枚举;代理

在后面的课程中我们将详细介绍这些数据类型。

概括来讲,你已经了解了什么是名称空间,如何定义自己的名称空间。如果你不想打入全称,可以使用"using"指示符。一旦你想缩短名称空间的长名,可以使用别名指示符。另外,除了类之外,你也了解了名称空间可以使用的其他一些数据类型。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值