What are namespaces in C#?

CsharpProgrammingServer Side Programming

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
raja
Published on 08-Aug-2018 07:01:00
Advertisements