Nested types
A type declared within a class or struct declaration is called a nested type. A type that is declared within a compilation unit or namespace is called a non-nested type. In the example using System; class A class B is a nested type because it is declared within class A, and class A is a non-nested type because it is declared within a compilation unit. 10.3.8.1 Fully qualified name The fully qualified name (§3.8.1) for a nested type is S.N where S is the fully qualified name of the type in which type N is declared. 10.3.8.2 Declared accessibility Non-nested types can have public or internal declared accessibility and have internal declared accessibility by default. Nested types can have these forms of declared accessibility too, plus one or more additional forms of declared accessibility, depending on whether the containing type is a class or struct: · A nested type that is declared in a class can have any of five forms of declared accessibility (public, protected internal, protected, internal, or private) and, like other class members, defaults to private declared accessibility. · A nested type that is declared in a struct can have any of three forms of declared accessibility (public, internal, or private) and, like other struct members, defaults to private declared accessibility. The example public class List public Node(object data, Node next) { private Node first = null; // Public interface public void AddToFront(object o) {...} public void AddToBack(object o) {...} public object RemoveFromFront() {...} public object RemoveFromBack() {...} public int Count { get {...} } declares a private nested class Node. 10.3.8.3 Hiding A nested type may hide (§3.7.1) a base member. The new modifier is permitted on nested type declarations so that hiding can be expressed explicitly. The example using System; class Base class Derived: Base class Test shows a nested class M that hides the method M defined in Base. 10.3.8.4 this access A nested type and its containing type do not have a special relationship with regard to this-access (§7.6.7). Specifically, this within a nested type cannot be used to refer to instance members of the containing type. In cases where a nested type needs access to the instance members of its containing type, access can be provided by providing the this for the instance of the containing type as a constructor argument for the nested type. The following example using System; class C public void F() { public class Nested public Nested(C c) { public void G() { class Test shows this technique. An instance of C creates an instance of Nested and passes its own this to Nested’s constructor in order to provide subsequent access to C’s instance members. 10.3.8.5 Access to private and protected members of the containing type A nested type has access to all of the members that are accessible to its containing type, including members of the containing type that have private and protected declared accessibility. The example using System; class C public class Nested class Test shows a class C that contains a nested class Nested. Within Nested, the method G calls the static method F defined in C, and F has private declared accessibility. A nested type also may access protected members defined in a base type of its containing type. In the example using System; class Base class Derived: Base class Test the nested class Derived.Nested accesses the protected method F defined in Derived’s base class, Base, by calling through an instance of Derived. 10.3.8.6 Nested types in generic classes A generic class declaration can contain nested type declarations. The type parameters of the enclosing class can be used within the nested types. A nested type declaration can contain additional type parameters that apply only to the nested type. Every type declaration contained within a generic class declaration is implicitly a generic type declaration. When writing a reference to a type nested within a generic type, the containing constructed type, including its type arguments, must be named. However, from within the outer class, the nested type can be used without qualification; the instance type of the outer class can be implicitly used when constructing the nested type. The following example shows three different correct ways to refer to a constructed type created from Inner; the first two are equivalent: class Outer<T> static void F(T t) { Outer<int>.Inner<string>.F(3, "abc"); // This type is different Outer.Inner<string>.F(t, "abc"); // Error, Outer needs type arg Although it is bad programming style, a type parameter in a nested type can hide a member or type parameter declared in the outer type: class Outer<T>
|