
.NET 1.1+.NET Namespaces (2)
The sixteenth part of the C# Object-Oriented Programming tutorial describes the use of namespaces. Namespaces allow classes, structures and other items to be grouped and organised and remove the possibility of class-naming conflicts.
Additive Namespace Declarations
Namespace declarations are additive. This means that the same namespace definition can be included in your code in multiple locations. Where two or more matching declarations are made, the contents of the two namespaces are combined into one. This is especially useful when an application can be deployed in different versions as the optional code can be placed in separate code files. The files can then be "swapped in" to create the various build configurations.
Adding the following code will extend the "FirstNamespace" namespace, adding a second class:
namespace FirstNamespace
{
class ShortTest
{
public void ShowShortMessage()
{
Console.WriteLine("First namespace!");
}
}
}
Referencing a Class in Another Namespace
Fully Qualified Names
Namespaces prevent conflicts between the names of classes by isolating the contents of each namespace. In the example created above, code created in the "Program" class cannot access the classes in the "FirstNamespace" and "SecondNamespace" namespaces by simply referring to the Test class. If the Main method of the program is updated as follows, the code will not compile because no "Test" class can be found:
static void Main(string[] args)
{
Test t = new Test(); // Does not compile
}
To address this problem, the fully qualified name of the class can be used. The fully qualified name is a name that is unique across the entire application, including the classes of the .NET framework itself. The name is constructed by simply prefixing the class name with the namespace that it exists within. The two names are separated by a full-stop (or period) character. The following updated code adds the namespace and compiles successfully.
static void Main(string[] args)
{
FirstNamespace.Test t = new FirstNamespace.Test();
}
The Using Directive
If you were required to prefix every use of every class, structure, enumeration, etc. with the name of the namespace that contained it, your code would become unwieldy. To avoid this the using directive can be used. You will already have seen this directive in action at the start of almost every code file that you have created.
The using directive tells the compiler that a namespace is in use by the code. When the compiler finds a class name that is not recognised in the current namespace, it instead checks each namespace defined in a using directive to see if the item exists there. This means that the fully qualified name for items within such a referenced namespace need not be used.
To add a using directive to the code, the using keyword and the name of the namespace are added at the start of the code file. For example, to add a reference to the FirstNamespace namespace, add the following line at the start of the code:
Now that the compiler knows that it is allowed to check inside "FirstNamespace" to match class names, the Main method can be updated to make it easier to read:
static void Main(string[] args)
{
Test t = new Test();
}
13 February 2008