Csharp 简明教程
C
namespace 被设计为提供一种将一组名称与另一组名称分开的途径。在一个命名空间中声明的类名称不会与在另一个命名空间中声明的相同类名称冲突。
A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another.
Defining a Namespace
命名空间定义以关键字 namespace 开头,后跟命名空间名称,如下所示 −
A namespace definition begins with the keyword namespace followed by the namespace name as follows −
namespace namespace_name {
// code declarations
}
要调用命名空间启用的函数或变量的版本,请预先添加命名空间名称,如下所示 -
To call the namespace-enabled version of either function or variable, prepend the namespace name as follows −
namespace_name.item_name;
以下程序演示如何使用命名空间 -
The following program demonstrates use of namespaces −
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();
}
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Inside first_space
Inside second_space
The using Keyword
using 关键字表示程序正在使用给定命名空间中的名称。例如,我们在程序中使用了 System 命名空间。类Console在其中定义。我们只需编写 -
The using keyword states that the program is using the names in the given namespace. For example, we are using the System namespace in our programs. The class Console is defined there. We just write −
Console.WriteLine ("Hello there");
我们可以写完全限定的名称,如下所示 -
We could have written the fully qualified name as −
System.Console.WriteLine("Hello there");
还可以使用 using 命名空间指令避免预先添加命名空间。此指令告诉编译器后续代码使用指定命名空间中的名称。因此,命名空间隐含在以下代码中 -
You can also avoid prepending of namespaces with the using namespace directive. This directive tells the compiler that the subsequent code is making use of names in the specified namespace. The namespace is thus implied for the following code −
让我们用使用指令重写先前的示例 -
Let us rewrite our preceding example, with using directive −
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();
}
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Inside first_space
Inside second_space
Nested Namespaces
可以像下面这样在一个命名空间内定义另一个命名空间 -
You can define one namespace inside another namespace as follows −
namespace namespace_name1 {
// code declarations
namespace namespace_name2 {
// code declarations
}
}
可以使用点(.)运算符像下面这样访问嵌套命名空间的成员 -
You can access members of nested namespace by using the dot (.) operator as follows −
using System;
using first_space;
using first_space.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();
}
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Inside first_space
Inside second_space