小白教程
所有教程
关于
Search
172.69.58.214
172.69.58.214
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“C Sharp 命名空间(Namespace)”的源代码
本页内容
上一节:
C_Sharp_接口(Interface)
下一节:
C_Sharp_预处理器指令
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:C Sharp 命名空间(Namespace)}}[[Category:C Sharp 教程|25]] = C Sharp 命名空间(Namespace) = '''命名空间'''的设计目的是提供一种让一组名称与其他名称分隔开的方式。在一个命名空间中声明的类的名称与另一个命名空间中声明的相同的类的名称不冲突。 我们举一个计算机系统中的例子,一个文件夹(目录)中可以包含多个文件夹,每个文件夹中不能有相同的文件名,但不同文件夹中的文件可以重名。 == 定义命名空间 == 命名空间的定义是以关键字 '''namespace''' 开始,后跟命名空间的名称,如下所示: <sample title="" desc="" lang="csharp" hererun="1"> namespace namespace_name { // 代码声明 } </sample> 为了调用支持命名空间版本的函数或变量,会把命名空间的名称置于前面,如下所示: <sample title="" desc="" lang="csharp" hererun="1"> namespace_name.item_name; </sample> 下面的程序演示了命名空间的用法: <sample title="" desc="" lang="csharp" hererun="1"> 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(); } } </sample> 当上面的代码被编译和执行时,它会产生下列结果: <sample title="" desc="" lang="csharp" hererun="1"> Inside first_space Inside second_space </sample> == using 关键字 == '''using''' 关键字表明程序使用的是给定命名空间中的名称。例如,我们在程序中使用 '''System''' 命名空间,其中定义了类 Console。我们可以只写: <sample title="" desc="" lang="csharp" hererun="1"> Console.WriteLine ("Hello there"); </sample> 我们可以写完全限定名称,如下: <sample title="" desc="" lang="csharp" hererun="1"> System.Console.WriteLine("Hello there"); </sample> 您也可以使用 '''using''' 命名空间指令,这样在使用的时候就不用在前面加上命名空间名称。该指令告诉编译器随后的代码使用了指定命名空间中的名称。下面的代码演示了命名空间的应用。 让我们使用 using 指定重写上面的实例: <sample title="" desc="" lang="csharp" hererun="1"> 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(); } } </sample> 当上面的代码被编译和执行时,它会产生下列结果: <sample title="" desc="" lang="csharp" hererun="1"> Inside first_space Inside second_space </sample> == 嵌套命名空间 == 命名空间可以被嵌套,即您可以在一个命名空间内定义另一个命名空间,如下所示: <sample title="" desc="" lang="csharp" hererun="1"> namespace namespace_name1 { // 代码声明 namespace namespace_name2 { // 代码声明 } } </sample> 您可以使用点(.)运算符访问嵌套的命名空间的成员,如下所示: <sample title="" desc="" lang="csharp" hererun="1"> 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"); } } } } </sample> 当上面的代码被编译和执行时,它会产生下列结果: <sample title="" desc="" lang="csharp" hererun="1"> In SomeNameSpace In Nested </sample>
返回至“
C Sharp 命名空间(Namespace)
”。
上一节:
C_Sharp_接口(Interface)
下一节:
C_Sharp_预处理器指令