C# 中的程序包


C# 语言使用命名空间代替 Java 中的程序包。

Java 中的程序包

我们使用 Java 中的程序包来防止命名冲突,控制访问权限,让搜索/查找和使用类、接口、枚举和注解变得更容易,等等。

C# 中的命名空间

命名空间旨在提供一种保持一组名称独立于另一组名称的方法。在某个命名空间中声明的类名与在其他命名空间中声明的相同类名不会冲突。

命名空间定义以关键字 namespace 开头,后接命名空间名称。以下内容阐释了如何在 C# 中使用命名空间 −

Learn C# in-depth with real-world projects through our C# certification course. Enroll and become a certified expert to boost your career.

示例

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();
   }
}

更新于: 2020 年 6 月 21 日

2 千次以上观看

开启你的职业生涯

完成课程获得认证

立即开始
广告