Export (0) Print
Expand All
3 out of 4 rated this helpful - Rate this topic

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();
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.