Home  >  Article  >  Backend Development  >  What are nested namespaces in C#?

What are nested namespaces in C#?

王林
王林forward
2023-08-29 10:57:021075browse

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

Namespaces within namespaces are called nested namespaces in C#. This is mainly done to structure your code correctly.

We have an external namespace-

namespace outer {}

Among them, we have an internal namespace within the external namespace-

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

}

Now we want to call the method of the internal namespace, Set the class object of the inner class and call the method as shown in the following example-

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

The above is the detailed content of What are nested namespaces in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete