C# nested classes

王林
王林forward
2023-09-13 10:21:03936browse

C# 嵌套类

A nested class is a class declared within another enclosing class. It is a member of its enclosing class, and members of the enclosing class cannot access members of the nested class.

Let’s look at an example code snippet of nested classes in C#.

Example
class One {
   public int num1;
   public class Two {
      public int num2;
   }
}
class Demo {
   static void Main() {
      One a = new One();
      a.num1++;
      One.Two ab = new One.Two();
      ab.num2++;
   }
}

This example shows that class Two is a nested class. Class two is contained within the class one declaration.

The class two here is included in the class one declaration. Therefore, the second class is a nested class. Because it has a public accessibility modifier, it can be accessed outside the scope of class One.

The above is the detailed content of C# nested classes. 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