Namespaces - using


If a program includes frequent references to the members of a particular namespace, the using statement can be used to make those references without qualifying each reference with the namespace name. The syntax for doing this is:

using name;

where name is the name of the namespace to be used. All members of the namespace are brought into view (i.e. they become part of the current namespace). using directives are specified at the top of a file, prior to other declarations.

The next example demonstrates how using may be employed in a program.

// Namespace3 - The using Directive

using System;
using NameSpace;

namespace NameSpace
{
    class X
    {
        public void Show()
        {
            Console.WriteLine("In X.Show in NameSpace");
        }
    }

    class Y
    {
        public void Show()
        {
            Console.WriteLine("In Y.Show in NameSpace");
        }
    }

}

class Program
{
    static void Main()
    {
        X x = new X();
        x.Show();

        Y y = new Y();
        y.Show();
    }
}

In Main(), X and Y do not need to be qualified with 'NameSpace'. Right near the top of the program, a using is employed to bring the members of NameSpace into view.

For the most part, applications are separated into namespaces with certain namespaces residing in their own dynamic link library (DLL). The above application can be separated along these lines. The definition of the namespace NameSpace and the definition of the classes X and Y can be placed in a DLL called NameSpaceModule. The source of NameSpaceModule is shown below.

// NameSpaceModule - Namespaces in a DLL

using System;

namespace NameSpace
{
    public class X
    {
        public void Show()
        {
            Console.WriteLine("In X.Show in NameSpace");
        }
    }

    public class Y
    {
        public void Show()
        {
            Console.WriteLine("In Y.Show in NameSpace");
        }
    }
}

This module is separately compiled as a DLL. The DLL is in the subdirectory bin/debug of the NameSpaceModule directory. Compile this DLL first. The application NameSpace3A then accesses this DLL. The source of NameSpace3A is shown below.

// Namespace3A - The using Directive
//             - The classes X and Y are now in the DLL NameSpaceModule
//             - Make Sure that a reference to NameSpaceModule is in effect
//             - See Project/Add Reference and use the Browse Tab

using System;
using NameSpace;

class Program
{
    static void Main()
    {
        X x = new X();
        x.Show();

        Y y = new Y();
        y.Show();
    }
}

Before the project NameSpace3A becomes usable, a reference to the DLL needs to be created. To do this take the Project pull down (in Visual Studio) and select Add Reference. In the Browse tab, add NameSpaceModule.dll to the references.

A Second Form of using

The using directive has a second form, shown below.

using alias = name;

alias becomes another name for the namespace or class referenced by name.

An example of this form of using is shown below.

// Namespace4 - The using Directive

using System;
using Alias = NameSpace.X;

namespace NameSpace
{
    class X
    {
        public void Show()
        {
            Console.WriteLine("In X.Show in NameSpace");
        }
    }
}

class Program
{
    static void Main()
    {
        Alias x = new Alias();
        x.Show();
    }
}

The alias is for the class NameSpace.X. Alias becomes another name for this class. Down in Main(), Alias is used to create a new object of the class.