Nested Types
- Nested type is declared within the scope of another type.
- It can access the enclosing type’s private members and everything else the enclosing type can access.
- It can be declared with the full range of access modifiers, rather than just public and internal.
- The default accessibility for a nested type is private rather than internal.
- Accessing a nested type from outside the enclosing type requires qualification with the enclosing type’s name (like when accessing static members).
- All types (classes, structs, interfaces, delegates and enums) can be nested inside
either a class or a struct.
public class TopLevel { public class Nested { } // Nested class public enum Color { Red, Blue, Tan } // Nested enum } static void Main() { TopLevel.Color color = TopLevel.Color.Red; } //Private member visibility public class TopLevel { static int x; public class Nested { public static void Foo() { Console.WriteLine (TopLevel.x); } } } static void Main() { TopLevel.Nested.Foo(); } //Protected member visibility public class TopLevel { protected class Nested { } } public class SubTopLevel : TopLevel { static void Foo() { new TopLevel.Nested(); } } static void Main() { }