Namespaces In C#

Namespaces

Namespaces are basically used to organize our code and to differentiate between the classes or methods with the same name. There are three ways to use namespaces.
  1. Using directives
  2. Using fully qualified name
  3. Using alias for namespaces
Using Directives

For invoking the  method from class A, we are using directive for declaring the namespace, as shown below.
  1. using project2;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using NameSpace1;  
  8.   
  9. namespace ConsoleApplication1  
  10. {  
  11.     class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             A a = new A();  
  16.             a.classAMethod();  
  17. }  
  18. }  
  19. }  
  20.   
  21. namespace NameSpace1  
  22. {  
  23.     namespace namespaceForClass1  
  24.     {  
  25.        public class A  
  26.         {  
  27.             public void classAMethod()  
  28.            {  
  29.                Console.WriteLine("I am method from Class A present in namespaceForClass1");  
  30.            }  
  31.         }  
  32.     }  
  33. }  
Fully Qualified name

Let's take an example to understand this.
  1. namespace NameSpace1  
  2. {  
  3.     namespace namespaceForClass1  
  4.     {  
  5.        public class A  
  6.         {  
  7.             public void classAMethod()  
  8.            {  
  9.                Console.WriteLine("I am method from Class A present in namespaceForClass1");  
  10.            }  
  11.         }  
  12.     }  
  13.     namespace namespaceForClass2  
  14.     {  
  15.         public class A  
  16.         {  
  17.             public void classAMethod2()  
  18.             {  
  19.                 Console.WriteLine("I am method from Class A present in namespaceForClass2");  
  20.             }  
  21.         }  
  22.     }  
  23. }  
As shown in the above example, there are two namespaces "namespaceForClass1" and "namespaceForClass2"
present in NameSpace1 and and these two namespaces contain classes with the same name.

So, if we need to call any of the classes from these two interfaces and we use namespaces by using directive, then we will get the ambiguity error, as shown in the following screenshot.


Then, how to differentiate between the two?

Yes! The answer is - using a namespace fully qualified name.
 
As shown in the below example, when we use NameSpace1.namespaceForClass1.A, then we have classAMethod available to invoke but when we use NameSpace1.namespaceForClass2.A, then we get classAMethod2 to invoke.
  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {           
  7.    NameSpace1.namespaceForClass1.A a1 = new NameSpace1.namespaceForClass1.A();  
  8.             a1.classAMethod();  
  9.   
  10.             NameSpace1.namespaceForClass2.A a2 = new NameSpace1.namespaceForClass2.A();  
  11.             a2.classAMethod2();  
  12. }  
  13. }  
  14. }  
  15.   
  16.   
  17.   
  18. namespace NameSpace1  
  19. {  
  20.     namespace namespaceForClass1  
  21.     {  
  22.        public class A  
  23.         {  
  24.             public void classAMethod()  
  25.            {  
  26.                Console.WriteLine("I am method from Class A present in namespaceForClass1");  
  27.            }  
  28.         }  
  29.     }  
  30.     namespace namespaceForClass2  
  31.     {  
  32.         public class A  
  33.         {  
  34.             public void classAMethod2()  
  35.             {  
  36.                 Console.WriteLine("I am method from Class A present in namespaceForClass2");  
  37.             }  
  38.         }  
  39.     }  
  40. }  
Namespace Alias

The alternative to the fully qualified name is an  alias for namespaces. Let's see how to use alias for namespaces.
  1. using project2;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using N1 = NameSpace1.namespaceForClass1;  
  8. using N2 = NameSpace1.namespaceForClass2;  
  9.   
  10. namespace ConsoleApplication1  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             
  17.             N1.A a= new N1.A();  
  18.             a.classAMethod();  
  19.             N2.A a1 = new N2.A();  
  20.             a1.classAMethod2();  
  21. }  
  22. }  
  23. }  
  24.   
  25.   
  26. namespace NameSpace1  
  27. {  
  28.     namespace namespaceForClass1  
  29.     {  
  30.        public class A  
  31.         {  
  32.             public void classAMethod()  
  33.            {  
  34.                Console.WriteLine("I am method from Class A present in namespaceForClass1");  
  35.            }  
  36.         }  
  37.     }  
  38.     namespace namespaceForClass2  
  39.     {  
  40.         public class A  
  41.         {  
  42.             public void classAMethod2()  
  43.             {  
  44.                 Console.WriteLine("I am method from Class A present in namespaceForClass2");  
  45.             }  
  46.         }  
  47.     }  
  48. }  
As in the above example when we used the namespaces from the same project assembly, we can use the namespaces from different project assemblies only by adding the reference dll of that project in our project.

Let's take an example of that as well. We have added a new project "project2" to the same solution and defined a class ABC containing print method.



Define a class ABC containing print method,
  1. namespace project2  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.         }  
  8.     }  
  9.   public  class ABC  
  10.     {  
  11.       public void print()  
  12.       {  
  13.           Console.WriteLine("I am invoked from project 2.");  
  14.       }  
  15.     }  
  16. }  
So, if we need to invoke this print method from "project2", let's see in the below screenshot what happens.



So, we did not find any class with name ABC and no options to add namespace for that. To get namespaces added from project2, we need to add the reference in our current project.

 
After adding the reference, we will have an option for adding the namespace from different projects.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using project2;  
  7.   
  8. namespace ConsoleApplication1  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             
  15.             
  16.   
  17.             ABC abc = new ABC();  
  18.             abc.print();  
  19. }  
  20. }  
  21. }  
Thank you and feel free to ask questions  in case of any doubts.