English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Namespaces (Namespace)

namespaceThe design purpose is to provide a way to separate a set of names from other names. The names of classes declared in one namespace do not conflict with the names of classes declared in another namespace.

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

Define a namespace

The definition of a namespace is with the keyword namespace Start, followed by the name of the namespace, as shown below:

namespace namespace_name
{
   // Code Declaration
}

To call functions or variables that support the namespace version, the name of the namespace is placed in front, as shown below:

namespace_name.item_name;

The following program demonstrates the usage of namespaces:

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 = new second_space.namespace_cl();
      fc.func();
      sc.func();
      Console.ReadKey();
   }
}

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

Inside first_space
Inside second_space

using keywords

using Keywords indicate that the program uses names from the specified namespace. For example, we use System The namespace that defines the Console class. 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 using Namespace directives, which make it unnecessary to prefix the namespace name when using. This directive tells the compiler that the subsequent code uses names from the specified namespace. The following code demonstrates the use of namespaces.

Let's use the using directive to specify the example above:

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 will produce the following result:

Inside first_space
Inside second_space

Nested Namespaces

Namespaces can be nested, which means you can define another namespace within a namespace, as shown below:

namespace namespace_name1 
{
   // Code Declaration
   namespace namespace_name2 
   {
     // Code Declaration
   }
}

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

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 will produce the following result:

In SomeNameSpace
In Nested