Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Namespace (Namespace)


May 11, 2021 C#


Table of contents


Namespace

Namespaces are designed to provide a way to separate a set of names from other names. The name of the class declared in one namespace does not conflict with the name of the same class declared in another namespace.

Define the namespace

The definition of a namespace starts with the keyword namespace, followed by the name of the namespace, as follows:

namespace namespace_name
{
   // 代码声明
}

In order to call a function or variable that supports the namespace version, the name of the namespace is placed first, as follows:

namespace_name.item_name;

The following program demonstrates the use 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 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

The using keyword

The using keyword indicates that the program is using a name in a given namespace. F or example, we use the System namespace in our program, which defines the class Console. We can just write:

Console.WriteLine ("Hello there");

We can write a fully qualified name, as follows:

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

You can also use the using namespace directive so that you don't have to add a namespace name to the front when you use it. T he instruction tells the compiler that subsequent code uses a name in the specified namespace. The following code delays the application of namespaces.

Let's use using using to specify to override the above 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 results:

Inside first_space
Inside second_space

Nested namespaces

Namespaces can be nested, i.e. you can define another namespace within one namespace, as follows:

namespace namespace_name1 
{
   // 代码声明
   namespace namespace_name2 
   {
     // 代码声明
   }
}

You can use the point (.) operator to access members of nested namespaces, 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