C# 中的嵌套命名空间是什么?

王林
王林 转载
2023-08-29 10:57:02 567浏览

C# 中的嵌套命名空间是什么?

命名空间内的命名空间在 C# 中称为嵌套命名空间。这样做主要是为了正确构建您的代码。

我们有一个外部命名空间 -

namespace outer {}

其中,我们在外部命名空间内有一个内部命名空间 -

namespace inner {
   public class innerClass {
      public void display() {
         Console.WriteLine("Inner Namespace");
      }
   }

}

现在要调用内部命名空间的方法,设置内部类的类对象并调用该方法,如下例所示 -

namespace outer {
   class Program {
      static void Main(string[] args) {
         innerClass cls = new innerClass();
         Console.WriteLine("Welcome!");
         Program.display();
         cls.display();
         Console.ReadLine();
      }

      public static void display() {
         Console.WriteLine("Outer Namespace");
      }
   }

   namespace inner {
      public class innerClass {
         public void display() {
            Console.WriteLine("Inner Namespace");
         }
      }
   }
}

以上就是C# 中的嵌套命名空间是什么?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除