Home  >  Article  >  Backend Development  >  [C# Tutorial] C# Namespace (Namespace)

[C# Tutorial] C# Namespace (Namespace)

黄舟
黄舟Original
2016-12-24 13:43:091061browse

C# Namespace

Namespace is designed to provide a way to separate a group of names 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 a namespace

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

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 Previously, 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 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 keyword

using keyword indicates The program uses the name in the given namespace. For 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 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 add the namespace name in front of it when using it. This directive tells the compiler that subsequent code uses names from the specified namespace. The following code delays the application of the namespace.

Let’s 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 namespaces

Namespaces can be nested, i.e. you One namespace can be defined within another namespace as follows:

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

You can access members of a nested namespace using the dot (.) operator 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 When executed, it will produce the following results:

Inside first_space
Inside second_space

The above is the content of [c# tutorial] C# namespace (Namespace). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn