C# Namespaces



We have been using namespaces earlier. We hade made a brief mention of namespace in the begining. It is time to take a closer look at namespaces

A namespace contains a number of classes within them. Let us rewrite our earlier example and put the Rectancle class in a namespace shape1.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace shape1
{
class Rectangle
{
    private int length, breadth;
    private int area;    

   
    public Rectangle(int length_input, int breadth_input)
    {
        length = length_input;
        breadth =  breadth_input;   
    }  
       
    public int calculatearea()
    {
        area = length*breadth;
        return (area);           
        }
}

}

namespace ClassRectangle
{
    class Program
    {
        static void Main(string[] args)
        {
            int area1;
            
            shape1.Rectangle rect1 = new shape1.Rectangle(10, 20);
            area1 = rect1.calculatearea();
            Console.Write("Area of Rectangle is ");
            Console.WriteLine(area1);
            Console.ReadLine();
        }
    }
}



If you run the above program you still get the same output as the previous program.

	
Area of Rectangle is 200



There are two changes this time. First we have put the class Rectangle within the namespace shape1. Secondly, take a look at the way the instances of the class is created with shape1.Rectangle.


shape1.Rectangle rect1 = new shape1.Rectangle(10, 20);


So, what is the use of namespace ? Suppose you want to have two sets of class with same class name. You can distinguish them by putting them under different namespace. Each set of class is differentiated by preceding it by namespace. Let us learn it by example. I have talked rectangle enough times. You have bored. Let us talk about cars. Take a look at the example in which we have put same class name Car within two namespaces US and Japan.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace US
{
class Car
{
    public String name;
}
       
}

namespace Japan
{
    class Car
    {
        public String name;
    }

}


namespace ClassCar
{
    class Program
    {
        static void Main(string[] args)
        {
                       
            US.Car car1 = new US.Car();
            car1.name = "Ford";

            Japan.Car car2 = new Japan.Car();
            car2.name = "Toyota";
            
       
            Console.Write("US Car is ");
            Console.WriteLine(car1.name);

            Console.Write("Japan Car is ");
            Console.WriteLine(car2.name);

            Console.ReadLine();
        }
    }
}



If you run the above program you still get the same output as the previous program.

	
US Car is Ford
Japan Car is Toyota




We have put the class Car in two different namespaces US and Japan. This is helpful when you have a number of classes and you want to distinguish them.

Namespaces help you organize the code.It also helps to prevent clashes between two sets of class names. Using namespace is a good programming habit. It is not necessary for small codes, like those we have been doing here. But in real life when the codes size bigger you can think of its benefits.