Local Inner Class in C#


A nested class is a class declared in another enclosing class and it has inner as well as outer class. It is a member of its enclosing class and the members of an enclosing class have no access to members of a nested class

Let us see an example code snippet of nested classes in C#.

Here, class Two is a local inner class −

Example

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

      One.Two xy = new One.Two();
      xy.num2++;
   }
}

The example shows that class Two is a nested class. Class Two is enclosed inside the class One declaration.

The class Two here is enclosed inside the declaration of class One. Class Two is thus a nested class. Because it has a public accessibility modifier, it can be accessed in places other than class One's scope.

Updated on: 22-Jun-2020

942 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements