// A public nested type can be used by anybody.
public class PublicInnerClass {}
// A private nested type can only be used by members
// of the containing class.
private class PrivateInnerClass {}
}
Although the syntax is clean, understanding why you might do this is not readily apparent.
To understand this technique, ponder the following traits of nesting a type:
• Nested types allow you to gain complete control over the access level of the inner type, as
they may be declared privately (recall that nonnested classes cannot be declared using the
private keyword).
• Because a nested type is a member of the containing class, it can access private members of
the containing class.
• Oftentimes, a nested type is only useful as a helper for the outer class, and is not intended for
use by the outside world.
When a type nests another class type, it can create member variables of the type, just as it
would for any point of data. However, if you wish to make use of a nested type from outside of the
containing type, you must qualify it by the scope of the nesting type. Consider the following code:
static void Main(string[] args)
{
Do'stlaringiz bilan baham: |