Skip to content

Latest commit

 

History

History
46 lines (29 loc) · 3.27 KB

File metadata and controls

46 lines (29 loc) · 3.27 KB
title description ms.date helpviewer_keywords ms.assetid
Nested Types
A type defined within a class, struct, or interface is called a nested type in C#.
02/08/2020
nested types [C#]
f2e1b315-e3d1-48ce-977f-7bae0960ba99

Nested Types (C# Programming Guide)

A type defined within a class, struct, or interface is called a nested type. For example

[!code-csharpDeclareNestedClass]

Regardless of whether the outer type is a class, interface, or struct, nested types default to private; they are accessible only from their containing type. In the previous example, the Nested class is inaccessible to external types.

You can also specify an access modifier to define the accessibility of a nested type, as follows:

The following example makes the Nested class public:

[!code-csharpPublicNestedClass]

The nested, or inner, type can access the containing, or outer, type. To access the containing type, pass it as an argument to the constructor of the nested type. For example:

[!code-csharpDeclareNestedInstance]

A nested type has access to all of the members that are accessible to its containing type. It can access private and protected members of the containing type, including any inherited protected members.

In the previous declaration, the full name of class Nested is Container.Nested. This is the name used to create a new instance of the nested class, as follows:

[!code-csharpUseNestedInstance]

See also