如何在 C# 中定义命名空间?
csharpprogrammingserver side programming更新于 2025/5/30 2:22:17
命名空间旨在提供一种将一组名称与另一组名称分开的方法。命名空间定义以关键字 namespace 开头,后跟命名空间名称,如下所示 −
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

