Easy Tutorial
❮ Csharp Interface Csharp Bitarray ❯

C# Namespace

Namespace is designed to provide a way to keep one set of names separate from another. The class names declared in one namespace do not conflict with the same class names declared in another.

Let's take an example from a computer system, where a folder (directory) can contain multiple subfolders, and each subfolder cannot have the same filename, but files in different folders can have the same name.

Defining a Namespace

A namespace is defined with the keyword namespace followed by the namespace name, as shown below:

namespace namespace_name
{
   // Code declarations
}

To call the version of the function or variable that has namespace support, the namespace name is used as a prefix, as shown below:

namespace_name.item_name;

The following program demonstrates the use of namespaces:

Example

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 result:

Inside first_space
Inside second_space

The using Keyword

The using keyword indicates that the program is using the names in the given namespace. For example, we are using the System namespace, where the Console class is defined. We can simply write:

Console.WriteLine("Hello there");

We can write the fully qualified name, as follows:

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

You can also use the using namespace directive, so you do not need to prefix the namespace name every time you use it. This directive tells the compiler that the subsequent code is making use of names in the specified namespace. The following code demonstrates the application of namespaces.

Let's rewrite the above example using the using directive:

Example

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 result:

Inside first_space
Inside second_space

Nested Namespaces

Namespaces can be nested, meaning you can define one namespace inside another, as shown below:

namespace namespace_name1 
{
   // Code declarations
   namespace namespace_name2 
   {
     // Code declarations
   }
}

You can access the members of nested namespaces using the dot (.) operator, as shown below:

namespace_name1.namespace_name2.item_name;
using System;
using SomeNameSpace;
using SomeNameSpace.Nested;

namespace SomeNameSpace
{
    public class MyClass 
    {
        static void Main() 
        {
            Console.WriteLine("In SomeNameSpace");
            Nested.NestedNameSpaceClass.SayHello();
        }
    }

    // Nested namespace
    namespace Nested    
    {
        public class NestedNameSpaceClass 
        {
            public static void SayHello() 
            {
                Console.WriteLine("In Nested");
            }
        }
    }
}

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

In SomeNameSpace
In Nested
❮ Csharp Interface Csharp Bitarray ❯