Home > Backend Development > C#.Net Tutorial > C# nested classes

C# nested classes

王林
Release: 2023-09-13 10:21:03
forward
935 people have browsed it

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++;
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template