What are namespaces in C#?


A namespace is for providing a way to keep one set of names separate from another. A namespace definition begins with the keyword namespace followed by the namespace name as follows −

namespace namespace_name {
   // code declarations
}

Define a Namespace −

namespace namespace_name {
   // code declarations
}

The following is an example showing how to use namespaces in C# −

Example

 Live Demo

using System;

namespace first_space {
   class namespace_cl {
      public void func() {
         Console.WriteLine("Inside first_space");
      }
   }
}

namespace second_space {
   class namespace_cl {
      public void func() {
         Console.WriteLine("Inside second_space");
      }
   }
}

class TestClass {
   static void Main(string[] args) {
      first_space.namespace_cl fc = new first_space.namespace_cl();
      second_space.namespace_cl sc = new second_space.namespace_cl();
      fc.func();
      sc.func();
      Console.ReadKey();
   }
}

Output

Inside first_space
Inside second_space

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 20-Jun-2020

153 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements