Adding Namespaces


More than one namespace declaration can exist for the same name. This allows namespaces to be split into files or separated within the same file. The compiler merges separate namespace declarations to form the ultimate name space. The merged namespace include all the declarations from the separate parts. The next program splits the generation of a namespace into two parts.

// Namespace5 - Adding Namespaces

using System;
using NameSpace;

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

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

Despite the namespace being spit into two parts, a single using directive brings both X and Y into view. The Main() routine then does not need to qualify X or Y when using them.