Latest web development tutorials

C # namespace (Namespace)

Namespace is designed to provide a way for a group name and other names separated manner.Name another namespace declaration in a namespace declared in the same class class name does not conflict.

Defining namespaces

It is defined namespace keywordnamespace, followed by a namespace name, as follows:

namespace namespace_name
{
   } // Code declares

In order to call support namespace version of a function or variable name will be placed in front of the namespace, as follows:

namespace_name.item_name;

The following program demonstrates the use of the namespace:

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 ();
   }
}

When the above code is compiled and executed, it produces the following results:

Inside first_space
Inside second_space

usingkeywords

using keyword indicates that the program uses a given space named after.For example, we use theSystem namespace in the program, which defines a class Console.We can only write:

Console.WriteLine ( "Hello there");

We can write the fully qualified name, as follows:

System.Console.WriteLine ( "Hello there");

You can also use theusing namespace directives, so when in use would not preceded by a namespace name.This directive tells the compiler code is then used to specify the namespace name. The following code delay the application namespace.

Let's use the example above using the specified rewrite:

using System;
using first_space;
using second_space;

namespace first_space
{
   class abc
   {
      public void func ()
      {
         Console.WriteLine ( "Inside first_space");
      }
   }
}
namespace second_space
{
   class efg
   {
      public void func ()
      {
         Console.WriteLine ( "Inside second_space");
      }
   }
}   
class TestClass
{
   static void Main (string [] args)
   {
      abc fc = new abc ();
      efg sc = new efg ();
      fc.func ();
      sc.func ();
      Console.ReadKey ();
   }
}

When the above code is compiled and executed, it produces the following results:

Inside first_space
Inside second_space

Nested namespaces

Namespaces can be nested, ie you can define another namespace within a namespace, as follows:

namespace namespace_name1 
{
   // Code declares namespace namespace_name2 
   {
     } // Code declares
}

You can use the dot operator to access a member of a nested namespace, as follows ():

using System;
using first_space;
using first_space.second_space;

namespace first_space
{
   class abc
   {
      public void func ()
      {
         Console.WriteLine ( "Inside first_space");
      }
   }
   namespace second_space
   {
      class efg
      {
         public void func ()
         {
            Console.WriteLine ( "Inside second_space");
         }
      }
   }   
}
 
class TestClass
{
   static void Main (string [] args)
   {
      abc fc = new abc ();
      efg sc = new efg ();
      fc.func ();
      sc.func ();
      Console.ReadKey ();
   }
}

When the above code is compiled and executed, it produces the following results:

Inside first_space
Inside second_space