名称空间用于组织类。它有助于在更大范围内控制方法和类的范围 网 编程项目。简单地说,您可以说它提供了一种方法,使一组名称(如类名)与其他名称不同。使用名称空间的最大优点是,在一个名称空间中声明的类名不会与在另一个名称空间中声明的类名冲突。它也被称为 命名类组 有共同特征的。命名空间的成员可以是 名称空间, 接口 , 结构 和 代表 .
定义名称空间
要在C#中定义名称空间,我们将使用 名称空间 关键字后跟名称空间的名称,以及包含名称空间主体的大括号,如下所示:
语法:
namespace name_of_namespace { // Namespace (Nested Namespaces) // Classes // Interfaces // Structures // Delegates }
例子:
// defining the namespace name1 namespace name1 { // C1 is the class in the namespace name1 class C1 { // class code } }
访问命名空间的成员
名称空间的成员可以使用点(.)操作人员C#中的类通过其各自的名称空间完全为人所知。
语法:
[namespace_name].[member_name]
注:
- 在一个程序中,可以在两个不同的名称空间中创建两个同名的类。
- 在命名空间中,任何两个类都不能具有相同的名称。
- 在C#中,类的全名从其名称空间名称开始,后跟 点(.) 运算符和类名,称为类的完全限定名。
例子:
// C# program to illustrate the // use of namespaces // namespace declaration namespace first { // name_1 namespace members // i.e. class class Geeks_1 { // function of class Geeks_1 public static void display() { // Here System is the namespace // under which Console class is defined // You can avoid writing System with // the help of "using" keyword discussed // later in this article System.Console.WriteLine( "Hello Geeks!" ); } } /* Removing comment will give the error because no two classes can have the same name under a single namespace class Geeks_1 { } */ } // ending of first namespace // Class declaration class Geeks_2 { // Main Method public static void Main(String []args) { // calling the display method of // class Geeks_1 by using two dot // operator as one is use to access // the class of first namespace and // another is use to access the // static method of class Geeks_1. // Termed as fully qualified name first.Geeks_1.display(); } } |
输出:
Hello Geeks!
在上面的例子中:
- 在里面 系统安慰WriteLine() ” “ 系统 “是一个命名空间,其中有一个名为” 安慰 “谁的方法是” WriteLine() “.
- 没有必要将C#中的每个类都保留在名称空间中,但我们这样做是为了更好地组织代码。
- 这里“ . “是用于将类名与命名空间、函数名与类名分开的分隔符。
using关键字
实际上,每次使用函数或类的完全限定名来调用函数或类(或者你可以说名称空间的成员)是不实际的。在上面的例子中, 系统安慰WriteLine(“你好,极客!”); 和 第一极客1。显示(); 是完全限定的名称。所以C#提供了一个关键字“ 使用 “这有助于用户避免反复写入完全限定的名称。用户只需在程序开始时提到名称空间名称,就可以轻松避免使用完全限定的名称。
语法:
using [namespace_name][.][sub-namespace_name];
在上述语法中, 点(.) 用于在程序中包含子名称空间名称。
例子:
// predefined namespace name using System; // user-defined namespace name using name1 // namespace having subnamespace using System.Collections.Generic;
节目:
// C# program to illustrate the // use of using keyword // predefined namespace using System; // user defined namespace using first; // namespace declaration namespace first { // name_1 namespace members // i.e. class class Geeks_1 { // function of class Geeks_1 public static void display() { // No need to write fully qualified name // as we have used "using System" Console.WriteLine( "Hello Geeks!" ); } } } // ending of first namespace // Class declaration class Geeks_2 { // Main Method public static void Main(String []args) { // calling the display method of // class Geeks_1 by using only one // dot operator as display is the // static method of class Geeks_1 Geeks_1.display(); } } |
输出:
Hello Geeks!
嵌套名称空间
还可以将一个名称空间定义为另一个名称空间,称为嵌套名称空间。要访问嵌套命名空间的成员,用户必须使用点(.)操作人员
例如 通用的 中的嵌套命名空间 收藏 命名空间为 系统收藏。通用的
语法:
namespace name_of_namespace_1 { // Member declarations & definitions namespace name_of_namespace_2 { // Member declarations & definitions . . } }
节目:
// C# program to illustrate use of // nested namespace using System; // You can also use // using Main_name.Nest_name; // to avoid the use of fully // qualified name // main namespace namespace Main_name { // nested namespace namespace Nest_name { // class within nested namespace class Geeks_1 { // Constructor of nested // namespace class Geeks_1 public Geeks_1() { Console.WriteLine( "Nested Namespace Constructor" ); } } } } // Driver Class class Driver { // Main Method public static void Main( string [] args) { // accessing the Nested Namespace by // using fully qualified name // "new" is used as Geeks_1() // is the Constructor new Main_name.Nest_name.Geeks_1(); } } |
输出:
Nested Namespace Constructor