HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

Namespaces in C#
Namespaces are used to organize your code and to create globally unique types there by avoiding name collisions. Namespaces are especially useful in large projects to avoid name collisions. Let us look at an example how namespaces can be used for organizing code and avoiding name collisions.
Let us take a Bank example. In this Bank we have 2 types of Customers
  • PersonalCustomer
  • EnterpriseCustomer
We have 2 Teams, Team A and Team B developing the Bank application.
  • Team A develop all the classes required for Personal Customers.
  • Team B develop all the classes required for Enterprise Customers.
These classes will finally be used in the main application. As Team A and Team B working are independently of each other it is very likely that both the Teams can have a class Customer to represent their type of customer. This could lead to name conflict(also called as name collisions). We cannot have two class(Types) with the same name in a single application. To solve this problem we can use namespaces as shown in the below example.
  • Team A which develops all the classes required for Personal Customers are with in PersonalCustomer namespace.
  • Team B which develops all the classes required for Enterprise Customers are with in EnterpriseCustomer namespace.
In our example both the Teams are using the same name Customer to represent their respective customer. Still we donot have a name collision because the classes are contained with in different namespaces. The fully qualified names prevent the name collisions.
  • Team A Customer fully qualified name : PersonalCustomer.Customer
  • Team B Customer fully qualified name : EnterpriseCustomer.Customer

using System;

class NameSpacesDemo

{

    public static void Main()

    {

        PersonalCustomer.Customer C1 = new PersonalCustomer.Customer("Prasad");

        EnterpriseCustomer.Customer C2 = new EnterpriseCustomer.Customer("Giri");

    }

}

                       

namespace PersonalCustomer

{

    class Customer

    {

        public Customer(string Name)

        {

            Console.WriteLine("Hello Personal Customer, Welcome " + Name);

        }

    }

}

                       

namespace EnterpriseCustomer

{

    class Customer

    {

        public Customer(string Name)

        {

            Console.WriteLine("Hello Enterprise Customer, Welcome " + Name);

        }

    }

}

Microsoft has made extensive use of namespaces to organise .NET frameweork classes. In our example in the first line we have using System directive. All the basic classes and types are present in this namespace System. For example the Console class we have used in our Main method is present in the System namespace. The fully qualified name of the Console class is System.Console.

We can also use the using directive for the namespaces we have created. For example to create an instance of our PersonalCustomer.Customer class we used.
  • PersonalCustomer.Customer C1 = new PersonalCustomer.Customer("Prasad");
Instead we can use the using directive as shown in the below code snippet.

using System;

using PersonalCustomer; // Using directive for our namespace

class NameSpacesDemo

{

    public static void Main()

    {

        //We need not use the fully qualified name to create the instance

        //as we are using the using PersonalCustomer; directive at the top.

        Customer C1 = new Customer("Prasad");

    }

}

                     

namespace PersonalCustomer

{

    class Customer

    {

        public Customer(string Name)

        {

            Console.WriteLine("Hello Personal Customer, Welcome " + Name);

        }

    }

}
Using an alias directive
Aliases offer great readability. For example, I have 10 using directives at the top of the program as shown in the below code snippet. In the Main method I am creating an instance of the DataSet class. Now the confusion is, to which namespace does the DataSet class belong? To avoid this type of confusion we can use aliases.

    using System;

    using System.CodeDom;

    using System.Data;

    using System.Globalization;

    using System.IO;

    using System.Net;

    using System.Reflection;

    using PersonalCustomer;

    using EnterpriseCustomer;

    class NameSpacesDemo

    {

        public static void Main()

        {

            //To which namespace above does the Datset

            //class belong to?

            DataSet DS = new DataSet();

        }

    }

Below code snippet shows how to declare and use alias directive. In this code snippet PC is an alias for PersonalCustomer namespace and EC for EnterpriseCustomer namespace. To create an istance of PersonalCustomer.Customer class, I have prefixed the class name with its respective alias.

    using System;

    using System.CodeDom;

    using System.Data;

    using System.Globalization;

    using System.IO;

    using System.Net;

    using System.Reflection;

    using PC = PersonalCustomer;  //PC alias for PersonalCustomer namespace

    using EC = EnterpriseCustomer;//EC alias for EnterpriseCustomer namespace

    class NameSpacesDemo

    {

        public static void Main()

        {

            PC.Customer C1 = new PC.Customer("Prasad");

            EC.Customer C2 = new EC.Customer("Giri");

        }

    }

Nesting namespaces
Having a namespace inside another namespace is called nesting namespaces. You can nest namespaces to any level you want. There are two ways to nest namespaces as shown below.
  • We have nested namespaces to 3 levels.The fully qualified name of Customer class in the below code snippet is A.B.C.Customer

    using System;

    namespace A

    {

        namespace B

        {

            namespace C

            {

                //Our Types(Classes,Interfaces,Structures,Delegates,Enumerations etc..)

                class Customer

                {

                    //Class code

                }

            }

        }

    }

  • Another approach to nest namespaces using the dot operator.

    using System;

    namespace A.B.C //Nesting namespaces using dot operator

    {

        //Our Types(Classes,Interfaces,Structures,Delegates,Enumerations etc..)

        class Customer

        {

            //Class code

        }

    }

Take awaya key points
  • Even if you do not explicitly declare a namespace, a default namespace is created. This unnamed namespace, sometimes called the global namespace, is present in every file. Any identifier in the global namespace is available for use in a named namespace
  • Namespaces have public access modifier by default. Explicit cccess modifiers and attributes are not allowed on namespace declarations. It is a compile time error to use explicit access modifiers or attributes on namespace declarations. We will talk about access modifiers and attributes in our later article.
  • Namespaces are used to organise your classes.
  • Namespaces allow you to create globally unique names to your classes.
  • Namespaces are especially useful in large projects to avoid name collisions.
  • Namespaces are containers of Types(Classes,Structures,Interfaces,Enumerations,Delegates)
  • Aliases can be used with namespaces to offer readability.
  • Namespaces can be nested to any levels deep.

Njoy Programming
   ByPrasad Cherukuri