Home >Backend Development >C#.Net Tutorial >C# nested classes
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#.
Exampleclass 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!