Types of Namespace in C#

Types of Namespace in C#

The namespaces are used in C# that contains collection of classes. There four types of namespaces in C#.

  1. Directive
  2. Station
  3. Alias
  4. Nested

Directive Namespace:

The directive namespace that is from link library and direct as link.

Eg:

  1. using system.IO    
  2. using System;    
  3. Console.WriteLine("Hai");    
  4. Console.WriteLine("Hello C# Corner!");  

Station Namespace:

The namespace that created automatically for project.

Eg: Console app create.

  1. using System.Xml;    
  2. //creating xml document automatically xml namespace will be added.    
  3. XmlDocument objXMLDoc = new XmlDocument();  

Alias Namespace:

The user defined name is created.

Eg:

  1. using xx system.IO;  
  2.   
  3. using System;  
  4. using hpy = System.Text.StringBuilder; //Alias namespace  
  5. class Program  
  6. {  
  7.     static void Main()  
  8.     {  
  9.      hpy Happy = new hpy();  
  10.      hpy.Append("smile");  
  11.      hpy.Append(100);  
  12.    
  13.      Console.WriteLine(Happy);  
  14.     }  
  15. }  

Nested Namespace:

This nested namespace hasa  namespace within another namespace.

Eg.
  1. Namespace Humanbeing  
  2. {  
  3.   
  4.     Class male  
  5.   
  6.     {  
  7.   
  8.         class female  
  9.   
  10.         {  
  11.   
  12.         }  
  13.   
  14.     }  
  15.   
  16.     namespace animal  
  17.   
  18.     {  
  19.   
  20.         class monkey  
  21.   
  22.         {  
  23.   
  24.         }  
  25.   
  26.     }  
  27.   
  28. }