How To Use Namespace In C#

Introduction

This article helps you to explain Namespace.
  1. How to use namespace in C#.
  2. Explain about  alias namespace in C#. 
  3. Explain about fully qualified namespace. 
  4. How to remove ambiguty error when mutltiple namesapces are created.  
How to use namespace In c#

First, we will discuss namespace. Namespace organizes your program. It makes code easily readable. It also provides assistance in avoiding name clashes.

First, we need to create C# console application. You can follow the steps to create a console application, using Visual Studio.

Open Visual studio ->File->New->Project after clicking project, a pop up Window will pop up , which is shown below.


 
 After opening a pop up under C# menu, I will discuss about sample code, using console Application. I chose console Application. Enter my Project name Namespacedemo after click OK. Solution Explorer will display.
  1. using System;    
  2. using Group.SubGroupA; /*Namespace directive For subGroup*/  
  3. namespace Namespacedemo    
  4. {    
  5.     class Program    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.     
  10.               
  11.           classGroup.print();    
  12.             /*Fully Qualified namespace*/    
  13.           Group.SubGroupB.classGroup.print();    
  14.             Console.ReadKey();    
  15.         }    
  16.     }    
  17. }    
  18.     
  19. namespace Group    
  20. {    
  21.     namespace SubGroupA    
  22.     {    
  23.         class  classGroup    
  24.         {    
  25.             public static void print()    
  26.             {    
  27.                 Console.WriteLine("Hi All Group A print The value ");    
  28.                   
  29.             }    
  30.         }    
  31.     }    
  32. }    
  33. namespace Group    
  34. {    
  35.     namespace SubGroupB    
  36.     {    
  37.         class classGroup    
  38.         {    
  39.             public static void print()    
  40.             {    
  41.                 Console.WriteLine("Hi All Group B print The value ");    
  42.                    
  43.             }    
  44.         }    
  45.     }    
  46. }    
In My Code, I will explain that I have one main Namespace, which is called with the name Group. Group has two subgroup Names- SubGroupA and SubGroupB, where both have void return type. I enter method name Print. Both two submenus have two different output strings. Now, I need to invoke both two namespaces in the main class of the Program. Now, I should invoke both SubGroupA and SubgroupB to main class. There are different ways to invoke namespace's main class Program.
  1. Full Qualified namespace
  2. Alias Namespace 
Proceed, as shown below and I have added namespace, using Directive namespace.
  1. using Group.SubGroupA;  /*Namespace directive For subGroup*/   
In Full Qualified namespace on needs to have  namespace name, classname and method.
  1. Group.SubGroupB.classGroup.print();   
Write the code, as shown below.  
  1. using System;  
  2. using GroupA = Group.SubGroupA; //Alias Name subgroupA  
  3. using GroupB = Group.SubGroupB; //Alias Name subgroupB  
  4. namespace Namespacedemo  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.   
  11.             GroupA.classGroup.print();  
  12.             GroupB.classGroup.print();  
  13.             Console.ReadKey();        
  14.         }  
  15.     }  
  16. }  
  17.   
  18. namespace Group  
  19. {  
  20.     namespace SubGroupA  
  21.     {  
  22.         class  classGroup  
  23.         {  
  24.             public static void print()  
  25.             {  
  26.                 Console.WriteLine("Hi All Group A print The value ");  
  27.                 
  28.             }  
  29.         }  
  30.     }  
  31. }  
  32. namespace Group  
  33. {  
  34.     namespace SubGroupB  
  35.     {  
  36.         class classGroup  
  37.         {  
  38.             public static void print()  
  39.             {  
  40.                 Console.WriteLine("Hi All Group B print The value ");  
  41.                  
  42.             }  
  43.         }  
  44.     }  
  45. }  
I use it, when I've got multiple namespaces with conflicting sub namespaces and/ or object names. Alias namespace avoids an ambiguty error. After running the code, the result page will look, as shown below.
 
Output


 
Conclusion

I hope you understood how to use namespace in C# and how to use alias Directive and Fully Qualified namespace in C#. Please share your valuable feedback and comments to improve my future articles.
Next Recommended Reading Nested Namespaces in C#