Nested Types (C# Programming Guide)

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

class Container
{
    class Nested
    {
        Nested() { }
    }
}

Regardless of whether the outer type is a class or a struct, nested types default to private, but can be made public, protected internal, protected, internal, or private. In the previous example, Nested is inaccessible to external types, but can be made public like this:

class Container
{
    public class Nested
    {
        Nested() { }
    }
}

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

public class Container
{
    public class Nested
    {
        private Container parent;

        public Nested()
        {
        }
        public Nested(Container parent)
        {
            this.parent = parent;
        }
    }
}

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:

Container.Nested nest = new Container.Nested();

See Also

Reference

Classes and Structs (C# Programming Guide)

Access Modifiers (C# Programming Guide)

Constructors (C# Programming Guide)

Concepts

C# Programming Guide