Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - NAMESPACES & USING


Namespace can be thought of as your personal space.


Say for example, you are working on a project. At the same time, you have a colleague who is also working on the same project.


And there is a scenario, where both of you have to create a class called Add.


But does C# allows you to create same class within one project?


Well! The answer is yes, but with Namespace.


Let us simplify with the below example.


Example :



namespace John 
{
    class Add
    {

        public int addition(int x, int y)
        {
    		int z = x + y;
    		return z;
        }
    }
}

namespace Paul 
{
    class Add
    {
        public int addition(int a, int b, int c)
        {
    		int z = a + b + c;
    		return z;
        }
    }
}

class MyApplication
{
    public static void Main(string[] arg) 
    {
        John.Add add1 = new John.Add();
        int result = add1.addition(5, 4);
        System.Console.WriteLine("The addded result for John's namespace is :"+result);
        
        Paul.Add add2 = new Paul.Add();
        int value = add2.addition(6, 2, 3);
        System.Console.WriteLine("The addded result for Paul's namespace is :"+value);
    }
} 


Output :



  The addded result for John's namespace is :9
  The addded result for Paul's namespace is :11

So, in the above example, there are two guys, John and Paul working in the same file.


And both of them needs to have a class called Add.


So, they have their own namespaces.


Namespace for 'John'


namespace John
{
	class Add
	{

		public int addition(int x, int y)
		{
			int z = x + y;
			return z;
		}
	}
}

Namespace for 'Paul'


namespace Paul
{
	class Add
	{
		public int addition(int a, int b, int c)
		{
			int z = a + b + c;
			return z;
		}
	}
}

Now, that both of them have their own namespace. How can they create their own objects of Add class?


In simple words, The namespace John has an Add class,


class Add
{

	public int addition(int x, int y)
	{
		int z = x + y;
		return z;
	}
}

Also namespace Paul has an Add class,


class Add
{
	public int addition(int a, int b, int c)
	{
		int z = a + b + c;
		return z;
	}
}

How would the Add object of John's namespace be different from Paul's namespace?


Well! The solution is using the namespace name itself.


And in the main method, we have created the object of Add class using John(i.e. namespace) followed by the classname.


John.Add add1 = new John.Add();

i.e. Instead of creating the object in the below way,


Add add1 = new Add();

We have created the object using the namespace name (i.e. John),


John.Add add1 = new John.Add();

And used the add1 object to calculate the result.


int result = add1.addition(5, 4);
System.Console.WriteLine("The addded result for John's namespace is :"+result);

Similarly, we have created the Add object for Paul's namespace.


Paul.Add add2 = new Paul.Add();
int value = add2.addition(6, 2, 3);
System.Console.WriteLine("The addded result for Paul's namespace is :"+value);

So, that was how you can create a namespace.


Now, let me tell you something interesting. So far, you were using namespace but unknowingly.


Guess where?


Let's see the below example,


Example :



class MyApplication
{
    public static void Main(string[] arg) 
    {
        System.Console.WriteLine("Hello World");
    }
} 


Output :



  Hello World

Any guesses so far?


Well! the print statement itself contains the namespace.


System.Console.WriteLine("Hello World");

System is the namespace in the above line.


So, in simple words,

  1. System is the namespace.

  2. Console is the class in that namespace.

  3. And WriteLine() is the method that belongs to the Console class.


Now, there is a simple way using which you don't have to write the namespaces every time.


It is by using the using keyword.


Writing namespace with 'using' keyword


Let us modify the above example,


Example :



using System;

class MyApplication
{
    public static void Main(string[] arg) 
    {
        Console.WriteLine("Hello World");
    }
} 


Output :



  Hello World

So, in the above example all we have done is, we have included the namespace System with the using keyword.


using System;

And in the print statement, we don't have to include System and just write,


Console.WriteLine("Hello World");