BlackWaspTM

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.

What is a Namespace?

As programming languages have evolved, the frameworks around them have greatly increased in size. The .NET framework provides a huge number of base classes, data types, algorithms, etc. If every one of these items had to be declared with a unique name, the naming task would be equally large and would leave very few good names available for the .NET developer.

To ensure that naming conflicts do not occur, either within the .NET framework or in your own programs, namespaces are used. A namespace simply provides a named group of classes, structures, enumerations, delegates, interfaces and other namespaces. Within the namespace, all declared items must be uniquely named. However, the same name may be duplicated in different namespaces. This means that if you decide to create a mathematics library you will be able to call it 'Math' even though Microsoft have also provided a Math library.

All definitions are created in a namespace, even if one is not explicitly declared. The .NET framework provides the default namespace, which is used automatically for any code that is written outside of any namespace declaration. This can be useful for short programs but has the drawback that all of the benefits that namespaces bring to organising your code are lost.

Declaring a Namespace

Syntax

Namespaces are declared using the namespace keyword. The basic syntax for creating a namespace is very simple, requiring only a name for the new grouping and a code block surrounded by braces:

namespace namespace-name {}

Namespace Example

To demonstrate the creation of a namespace we will create some code. To begin, create a new console application named "NamespaceDemo". If you are creating the project using Visual Studio, you will notice that the Program class that is automatically generated exists within a namespace that is also called "NamespaceDemo".

It is possible to define more than one namespace within the same code file. In this case, we will add our new namespace in the same file as the automatically created one. Add the following namespace and contained class as the foot of the code, outside of the code block of the existing namespace:

namespace FirstNamespace
{
    class Test
    {
        public void ShowMessage()
        {
            Console.WriteLine("This is the first namespace!");
        }
    }
}

This code creates a new namespace named "FirstNamespace" and defines a "Test" class containing a single method that outputs a string to the console. We can now add a second namespace, again at the foot of the code:

namespace SecondNamespace
{
    class Test
    {
        public void ShowMessage()
        {
            Console.WriteLine("This is the second namespace!");
        }
    }
}

The second namespace also includes a class named "Test". This does not cause a naming conflict because the two classes are logically separated by the namespaces. This means that the program will compile without error.

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!");
        }
    }
}
13 February 2008