Nested Types
A nested type is declared within the scope of another type. For example:
public class TopLevel
{
public class Nested { } // Nested class
public enum Color { Red, Blue, Tan } // Nested enum
}A nested type has the following features:
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
publicandinternal.The default accessibility for a nested type is
privaterather thaninternal.Accessing a nested type from outside the enclosing type requires qualification with the enclosing type’s name (like when accessing static members).
For example, to access Color.Red
from outside our TopLevel class, we’d
have to do this:
TopLevel.Color color = TopLevel.Color.Red;
All types can be nested; however, only classes and structs can nest.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access