#628 – Why You Might Create a Nested Type

The most common reasons for creating a nested type–one type defined within another one–are:

  • For information hiding purposes, so only the outer type has access to the type being defined
  • As a way of organizing your code, associating a sub-type with the parent type where it is used

One scenario might be that you define several subclasses inside of a parent class and make the subclasses private.  You might also use the parent class as a factory, returning instances of the subclass (but visible to client through the parent class’ type).  Doing this guarantees that no code outside of the parent class can directly work with the subclasses, but has to go through the parent class to instantiate them.

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: