C# Tutoriallogin
C# Tutorial
author:php.cn  update time:2022-04-11 14:06:23

C# namespace (Namespace)



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

Define 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 will be placed in front, as follows:

namespace_name.item_name;

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

using Keywords

using Keywords indicate procedures The name in the given namespace is used. For example, we use the System namespace in our program, where the class Console is defined. We can just 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 that you don’t need to Prepend the namespace name. This directive tells the compiler that subsequent code uses names from the specified namespace. The following code delays the application of the namespace.

Let us rewrite the above example using the using specification:

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

Inside first_space
Inside second_space

Nested naming Spaces

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

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

You can access nesting using the dot (.) operator Set the members of the 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 will produce the following results:

Inside first_space
Inside second_space

php.cn