NenTang.vn
.NETLập trình căn bản C#
Namespace trong C#
Hướng đối tượng trong C#
Thời lượng: 150 phútĐọc khoảng: 3 phút
Từ viết tắt
Nội dung bài học không khai báo từ viết tắt bằng thẻ abbr.
Nội dung bài học
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.
To call the namespace-enabled version of either function or variable, prepend the namespace name as follows −
The following program demonstrates use of namespaces −
When the above code is compiled and executed, it produces the following result −
We could have written the fully qualified name as −
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 −
When the above code is compiled and executed, it produces the following result −
You can access members of nested namespace by using the dot (.) operator as follows −
When the above code is compiled and executed, it produces the following result −
Defining a Namespace
A namespace definition begins with the keyword namespace followed by the namespace name as follows −shellscript
shellscript
csharp
shellscript
The using Keyword
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 −shellscript
shellscript
csharp
shellscript
Nested Namespaces
You can define one namespace inside another namespace as follows −csharp
csharp
shellscript

