C# 中的命名空间是什么?
csharpprogrammingserver side programming更新于 2025/10/15 11:07:17
命名空间用于将一组名称与另一组名称区分开来。命名空间的定义以关键字 namespace 开头,后跟命名空间名称,如下所示 −
namespace namespace_name {
// 代码声明
}
定义命名空间 −
namespace namespace_name {
// 代码声明
}
以下示例展示了如何在 C# 中使用命名空间−
示例
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();
}
}
输出
Inside first_space Inside second_space

