This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

.NET Namespaces

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.

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:

using FirstNamespace;

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();

Creating Aliases

The using directive as demonstrated above can provide much neater code. However, where two using directives are included and each referenced namespace contains a matching class name, a name conflict is created. To show this problem, add a second using directive to the code referencing the SecondNamespace namespace:

using SecondNamespace;

Now that both of the additional namespaces are referenced the compiler does not know which Test class to use when creating the object in the Main method. Attempting to compile the program generates an error that explains that "Test" is now an ambiguous reference.

This problem could be resolved by simply providing the fully qualified name when using the Test classes. However, namespace names can be very long and this presents the original problem of making the code more difficult to read. Instead, the using directive can be used to create an alias. This allows a shorter name to be linked to a namespace and to be used instead of the namespace to qualify the class.

To create an alias, the following syntax is used:

using alias-name = namespace;

Alias-name is the name of the alias as it will appear elsewhere in the code. Namespace is the namespace to create an alias for. To apply aliases for the two namespaces, modify the using directives as follows:

using First = FirstNamespace;
using Second = SecondNamespace;

Adding the aliases removes the ability to reference the classes without providing either the alias of the fully qualified name. To make it possible to compile the program again, modify the Test object declaration in the Main method to add an alias:

First.Test t = new First.Test();
13 February 2008