What does namespace mean in .NET?

A namespace is a very simple concept. Within your program code and within the code that forms the .NET libraries, names have to be given to lots of things — data types, variables, and blocks of code called functions all have to have names. The problem is that if you happen to invent a name that is already used in the library, there ’ s potential for confusion. A namespace provides a way of getting around this problem.

All the names in the library code defi ned within the System namespace are implicitly prefi xed with the namespace name. So a name such as String in the library is really System::String . This means that if you have inadvertently used the name String for something in your code, you can use System::String to refer to String from the .NET library without confusing it with the name String in your code.

The two colons ( :: ) are an operator called the scope resolution operator . Here the scope resolution operator separates the namespace name System from the type name String . You have seen this operator in the native C++ examples earlier in this chapter with std::cout and std::endl. This is the same story — std is the namespace name for native C++ libraries, and cout and endl are the names that have been defined within the std namespace to represent the standard output stream and the newline character, respectively.

In fact, the using namespace statement in this example enables you to use any name from the System namespace without having to use the namespace name as a prefi x. If you did end up with a name confl ict between a name you defi ned and a name in the library, you could resolve the problem by removing the using namespace statement and explicitly qualifying the name from the library with the namespace name.

6月 16, 2011

You Might Also Like

0 comments