Nested Types


Classes are not restricted to holding data and function members. They can also contain the definitions of types like classes, structs, enums, and interfaces, which then are referred to as nested types. This section only deals with nested classes, but the syntax is so similar and straightforward that it easily can be adjusted to work with the remaining three kinds of types (see the notes in Syntax Box 14.5).

As Syntax Box 14.5 shows, a class is nested inside an outer class by simply writing the class header and its associated block as usual, but inside the block of the outer class.

Syntax Box 14.5 The Nested Class

 [<Access_modifier>] class <Identifier_outer_class> {     [<Outer_class_data_members>]     [<Outer_class_function_members>]     [<Access_modifier>] class <Identifier_nested_class>     {         [<Inner_class_data_members>]         [<Inner_class_function_members>]         [<Inner_class_nested_types>]     } } 


  • A nested class can itself contain other nested classes, which again can contain other nested classes, and so on.

  • Structs, enums and interfaces are turned into nested types by following the same principle as with classes; just write the definition as usual but place it inside the class block of the outer class.

By nesting a class, we can confine its visibility to the scope of its outer class. This is done with the same access modifiers that are used with the other class members (private, public, and so on) and with similar consequences. A private nested class can only be accessed from within the block of its outer class, whereas public exposes it beyond the class scope. Just as a method is made private because it is only needed within its class, a class is nested and declared private because it is so specialized and integrated with its outer class that no other part of the program would ever request its services.



Nested classes often form composition relationships with their outer classes.

The Advantages of Nested Classes

Nested classes effectively say "Not for reuse purposes." Consequently they help reduce the number of classes on which programmers need to focus when they sift through the code to find classes for use in their own program parts.

Nested classes also let you organize your code effectively, because the inner class always follows its outer class. It is easier to move and keep count of one main class with its nested classes inside instead of several classes floating around independently in the source code.

A Simple Nested Class Example

It's time to have a look at a simple example to illustrate nested classes. Recall the Elevator class from our elevator simulation in Listing 5.1 of Chapter 5, "Your First Object-Oriented C# Program." We might decide to expand this program to contain an ElevatorEngine class that has the ability to control the exact speed of the Elevator, while enabling us to simulate the wear and tear of some of the ElevatorEngine's inner parts. Its relationship with the Elevator is intimate, and its services are so specialized that we decide to make the ElevatorEngine a nested private class inside the Elevator class. The important code fragments for doing this are shown in Figure 14.7.

Figure 14.7. A nested class and its outer class.

The Person class is an arbitrary class that has merely been included to illustrate the inability to use the ElevatorEngine class from outside the Elevator class.

In most cases, however, classes are not as closely related as the ElevatorEngine and the Elevator classes of our example, so access is needed to the majority of classes. To organize our classes in an orderly, systematic, hierarchical fashion providing easy access, we can use namespaces. The next chapter takes a closer look at how you can create and use your own namespaces.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: