Translate

Thursday, 25 May 2017

C# Namespaces Tutorial

Topic : Namespaces in C#
Q 1.) First question will come in mind is what are namespaces and why it came in existence?

Before answering this question let me ask you something : 

Q 2.) Can you declare two classes with same name in one file?
You will say NO.
See the code and compile it: 
using System;
class MainClass{
    static void Main(string[] args){
        Console.WriteLine("Hello World");
    }
}
class MainClass{
}

Output(After compilation you will get  error as mentioned below) : 
prog.cs(7,7): error CS0101: The namespace `global::' already contains a definition for `MainClass'
prog.cs(2,7): (Location of the symbol related to previous error)
Compilation failed: 1 error(s), 0 warnings

Q 2.) How can we declare two classes with same name in one file only?
Ans) So here come the concept of namespaces, Yes we can declare with the help of namespaces 
 See the below code and run it in your system. You will get to know that we can declare two classes with same name in one file with the help of namespaces:
using System;
namespace main_class{
class MainClass{
    static void Main(string[] args){
        Console.WriteLine("Hello World");
    }
}
}
namespace again_main_class{
        class MainClass{
        }
    }

Output: Hello World




Q 4.) What are Namespaces in C#?
Ans. ) As we discussed above There are some definitions of namespaces like:
1. It is collection of classes. 
As you can see in both above examples that it can contain as many classes it want.
2. It is used to differentiate one set of name from another set of name. 
See the below code for explanation: 

using System;
namespace main_class{
class MainClass{
    static void Main(string[] args){
        Console.WriteLine("Hello World");
    }
    
    void fun(){
        Console.Write("this is main_class namespace fun");
    }
}
}
namespace again_main_class{
        class MainClass{
            void fun(){
                Console.Write("this is again_main_class namespace fun");
                }
        }
    
    }

So in the above code you can see that we are having fun() two times but in different namespace .so by this way we can say that it is used to differentiate between one set of name from another set of name.



So I think that you got what are namespaces and why we used them.

Now Let me start how to use namespaces and member inside namespace 

Let me explain with the help of one code: 

using System;
namespace main_class{
class MainClass{
    static void Main(string[] args){
        Console.WriteLine("Hello World");
    }
    
    void fun(){
        Console.Write("this is main_class namespace fun");
    }
}
}

Explanation: 
1. namespace main_class{
}

This is known as namespace declaration where.
namespace : keyword
main_class: name of namespace

2. namespace contain one class MainClass and MainClass contain two methods fun() and main(). So I think there is nothing to explain in that.
3. What is this line using System
So here System is the namespace name, which contain Console class.
So in this example we are using Console.WritelLine() thats why we have to include System namespace.

Now Let me tell you one thing: 
What if I will not include this System namespace then how can I use this Console class?

Yes I can use this class without writing using System as below:

System.Console.WriteLine();


Let me explain with the help of one more code that how to access member of namespaces: 

using System;

namespace first_namespace{
     class First{
        public void fun(){
            Console.WriteLine("hi this is inside first_namespace");
        }
    }
}

namespace second_namespace{
     class Second{
        public void fun(){
            Console.WriteLine("hi this is inside second_namespace");
        }
    }
}

namespace main_class{
class MainClass{
    static void Main(string[] args){
        Console.WriteLine("Hello World");
        
        first_namespace.First obj=new first_namespace.First();
        obj.fun();
        second_namespace.Second second=new second_namespace.Second();
        second.fun();
        
    }
    
    void fun(){
        Console.Write("this is main_class namespace fun");
    }
}
}



Explanation: 
1.As you can see we  have fun() in both the namespaces, so to access fun() we have to use namespace name at the time of object creation.

Means:
Namespace_name.class_name object_name= new Namespace_name.class_name();

3. To avoid this namespace_name.something we can include that namespace name at the top just like using System
Example: In above example we can write our code as follows also:

using System;
using first_namespace;
using second_namespace;

namespace first_namespace{
     class First{
        public void fun(){
            Console.WriteLine("hi this is inside first_namespace");
        }
    }
}

namespace second_namespace{
     class Second{
        public void fun(){
            Console.WriteLine("hi this is inside second_namespace");
        }
    }
}

namespace main_class{
class MainClass{
    static void Main(string[] args){
        Console.WriteLine("Hello World");
        
        First obj=new First();
        obj.fun();
        Second second=new Second();
        second.fun();
        
    }
    
    void fun(){
        Console.Write("this is main_class namespace fun");
    }
}
}




So Thanks guys I hope now you have idea about namespaces in C#. if any doubt is there or you want to share some information about namespaces then please share in the comment.

No comments:

Working With Java Collections