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.

Nested Namespaces

We have seen how classes and other items can be declared within a namespace to provide grouping of code items and isolation of names to prevent conflicts. In addition, namespaces may be created within other namespaces to create a nested categorisation. This allows for deep tree structures to be created and for great control over the organisation of code modules.

Consider the following nested namespace declarations:

namespace Parent
{
    namespace Child
    {
        namespace Grandchild
        {
            class Test
            {
                public void ShowMessage()
                {
                    Console.WriteLine("This is a nested namespace!");
                }
            }
        }
    }
}

In this situation there are three levels of namespace with each declared inside its parent. The "Test" class is declared within the third level of nesting. To access this class using its fully qualified name, all three namespaces must be specified with each prefixing its child and separated using a full stop. The fully qualified name for the Test class is therefore "Parent.Child.Grandchild.Test".

As with single layer namespaces, a using directive can be added to remove the requirement to use the fully qualified name. In this case, the using directive would be:

using Parent.Child.Grandchild;

A shorter notation exists for declaring a nested namespace when classes and other declarations only exist at the deepest level of the hierarchy. Rather than define each namespace individually, the namespaces can be created in a single statement, again with each namespace separated by a full stop. The following code is functionally equivalent to the previous nested namespaces sample:

namespace Parent.Child.Grandchild
{
    class Test
    {
        public void ShowMessage()
        {
            Console.WriteLine("This is a nested namespace!");
        }
    }
}

Namespace Naming Conventions

To avoid conflicts with the .NET framework's namespaces and those of the many third-party component vendors, Microsoft has proposed a naming convention. The convention should be used for all of the namespaces that you create.

A suggested namespace should be of the format:

CompanyName.TechnologyName.Feature.Design

In this naming convention, CompanyName is the name of the company or brand that owns the code. For long company names, this should be reduced in size but standardised across the organisation. TechnologyName refers to the high-level technology that classes within the namespace relate to. For example, "Data" for database-related items or "Xml" for XML functionality.

Feature is an optional element that gives a name to a feature within a technology namespace. Finally, the namespace "Design" is suggested for namespaces that contain design-time functionality. An example namespace to contain the design-time elements of custom media controls for DVD movies could be:

BlackWasp.Media.Dvd.Design
13 February 2008