A nested type is declared within the scope of another type.
For example:
public class Main { public class Nested { } // Nested class public enum Color { Red, Blue, Tan } // Nested enum }
A nested type can access the enclosing type's private members and everything else the enclosing type can access.
A nested type can be declared with the full range of access modifiers.
The default accessibility for a nested type is private
.
Classes, structs, interfaces, delegates and enums can be nested inside either a class or a struct.
Here is an example of accessing a private member of a type from a nested type:
public class Main { static int x; class Nested { static void Foo() { Console.WriteLine (Main.x); } } }
Here is an example of applying the protected access modifier to a nested type:
public class Main { protected class Nested { } } public class SubMain : Main { static void Foo() { new Main.Nested(); } }
Here is an example of referring to a nested type from outside the enclosing type:
public class Main { public class Nested { } } class Test { Main.Nested n; }