Namespace cung cấp cách giữ một tập tên tách biệt khỏi tập khác. Định nghĩa namespace bắt đầu bằng từ khóa namespace theo sau tên namespace:
namespace namespace_name {
// code declarations
}
Định nghĩa Namespace:
namespace namespace_name {
// code declarations
}
Ví dụ sử dụng namespaces:
Ví dụ
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();
}
}
Kết quả
Inside first_space
Inside second_space