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. using System;
  3. using hpy = System.Text.StringBuilder; //Alias namespace
  4. class Program
  5. {
  6. static void Main()
  7. {
  8. hpy Happy = new hpy();
  9. hpy.Append("smile");
  10. hpy.Append(100);
  11. Console.WriteLine(Happy);
  12. }
  13. }

Nested Namespace:

This nested namespace hasa namespace within another namespace.

Eg.
  1. Namespace Humanbeing
  2. {
  3. Class male
  4. {
  5. class female
  6. {
  7. }
  8. }
  9. namespace animal
  10. {
  11. class monkey
  12. {
  13. }
  14. }
  15. }