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.

C# Programming
.NET 2.0+

C# Namespace Alias Qualifier

Namespaces are ideal for organising your classes and structures. As software increases in size, the probability of namespace and class names colliding and causing ambiguity problems increases. The namespace alias qualifier decreases this possibility.

Namespace Aliases

In the article, ".NET Namespaces", I described how namespaces can be used to help organise your code. The article also examines the use of namespace aliases, which allow replacement of namespace names with aliases to prevent ambiguous definitions. In larger projects, particularly those that employ many third-party libraries, these name collisions can pose a problem.

The following code shows an example of an ambiguous declaration. In this sample, two namespaces exist, each containing a class named "Test". As both namespaces are referenced in a basic using statement, the attempt to create an instance of Test from within the Main method is ambiguous and the program will not compile.

using System;
using FirstNamespace;
using SecondNamespace;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            test.ShowMessage();
        }
    }
}


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


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

The simple way to solve this problem is to use the fully qualified name of the Test class to be instantiated. However, if the namespace name is very long, this can reduce the readability of your code. Instead, you may decide to use an alias for each of the two additional namespaces. To do so, you could change the two using directives as follows:

using first = FirstNamespace;
using second = SecondNamespace;

Now the creation of the Test object can be changed to use the alias, causing the code to compile correctly.

first.Test test = new first.Test();

Namespace Alias Qualifier

The problem described above can become more complicated. If a third namespace were added and this had a name matching a current namespace alias, a new opportunity for ambiguity arises. For example, try adding this third namespace at the foot of the above program:

namespace first
{
    class Test
    {
        public static void ShowMessage()
        {
            Console.WriteLine("This is yet another namespace!");
        }
    }
}

With this namespace in place, the code again fails to compile, even though the Test object's declaration is now fully qualified. This is due to the new collision between the "first" alias and the "first" namespace. You could resolve this by changing the namespace alias name. Alternatively, if you are developing using the .NET framework version 2.0 or later, you can use the namespace alias qualifier. The qualifier appears as two colons (::) with the namespace or alias name to the left and the class to be used to the right.

alias-name::class-name

If we use this syntax when declaring the Test object, we can specify that the "first" alias is to be used, creating an object from FirstNamespace. Change the declaration as follows and execute the code to see that this indeed happens:

first::Test test = new first::Test();

NB: The above sample is for example purposes only. In this situation you would be advised to change the alias name.

Global Namespace Alias

If a namespace has no alias, the namespace alias qualifier may still be used to identify it. In this case, the alias to the left of the qualifier symbol should be specified as "global", referencing the global or default namespace. The class name to the right of the qualifier can then be fully qualified. In the above example, to reference the "first" namespace, change the Test object declaration as follows:

global::first.Test test = new global::first.Test();

You can test that the correct namespace is referenced by executing the program.

8 July 2008