Printer Friendly Version      Send     
Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio .NET
Component Authoring
Complex Components
 Nested Classes in Components
Visual Basic and Visual C# Concepts
Nested Classes in Components

Many components are fairly simple. They might consist of a single class with a simple object model or no object model. Other components are more complex, and might need to contain and manage a large number of subordinate objects. Nested classes are one way for complex components to contain and manage the objects they need.

A nested class is a class that is fully enclosed within another class declaration. For example, a nested class and an enclosing class might look like the following example:

' Visual Basic
' This is the enclosing class, whose class declaration contains the nested
' class.
Public Class EnclosingClass 
   ' This is the nested class. Its class declaration is fully contained 
   ' within the enclosing class.
   Public Class NestedClass
   ' Insert code to implement NestedClass.
   End Class
' Insert code to implement EnclosingClass.
End Class

// C#
// This is the enclosing class, whose class declaration contains the
// nested class.
public class EnclosingClass
{
   // This is the nested class. Its class declaration is fully contained
   // within the enclosing class.
   public class NestedClass
   {
      // Insert code to implement NestedClass.
   }
   // Insert code to implement EnclosingClass.
}

In this example, the class declaration for NestedClass is fully contained by the class declaration of EnclosingClass.

As a result of being contained within the enclosing class, the nested class gains a certain level of protection. Unless you use the Imports (using in C#) statement, all references to the nested class must be qualified with the name of the containing class. For example, to instantiate the nested class in the previous example, you would have to use the following syntax:

' Visual Basic
Dim aClass as New EnclosingClass.NestedClass()

// C#
EnclosingClass.NestedClass aClass = new EnclosingClass.NestedClass();

The access level of the nested class is implicitly at least the access level of the enclosing class. Even if the nested class is Public, if the enclosing class is Friend (internal in C#), then only members of the assembly will be able to access the nested class, and if the enclosing class is Private, then the nested class will be unavailable to all callers except the enclosing class.

Assuming a Public enclosing class, the access level of the nested classes pretty much follows the same rules as for access of unnested classes. Friend classes are available to members of the assembly, but not external clients. Private classes are available to the enclosing class, other nested classes within the enclosing class, and any classes nested within other nested classes.

Nested classes are useful when an object will logically contain subordinate objects, but other objects would have use for those objects. An example might be a Wheel class. This could be a class that clients could create and use wherever a wheel might be needed in their application. But except in the most primitive implementations, a wheel is not just a single object, but is composed of several subordinate objects, each of which build the wheel. A wheel might have a Rim object, Tire object, Spoke objects and other objects, without which the wheel could not function. But the average user would have no need to create a spoke, or a rim, or a bearing — all he's interested in is the wheel.

In a case like this, it makes sense for the Wheel class to contain the implementation for all of its subordinate classes. This way, the wheel can create and manage any contained objects it may need while conveniently hiding the details of this implementation from the client. Those subordinate objects the client might reasonably need to have contact with now and again (for example, a Tire object) can be exposed as part of the public object model, and those that a client should never see (for example, a Bearings collection) could be declared private and hidden.

See Also

Recommendations on Nested Classes in Components | Implementing Nested Classes | Components that Contain Other Components

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker