Partager via


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 types 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 m_parent;

        public Nested()
        {
        }
        public Nested(Container parent)
        {
            m_parent = parent;
        }
    }
}

Nested types can access private and protected members of the containing type, including any inherited private or 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

Objects, Classes and Structs (C# Programming Guide)
Access Modifiers (C# Programming Guide)

Concepts

C# Programming Guide

Other Resources

Constructors (C# Programming Guide)