Home > Articles > Programming > C#

  • Print
  • + Share This
This chapter is from the book

Nested Classes

A nested class is one that is fully enclosed, or nested, inside another class declaration. Nested classes are a convenient way to allow an outer class to create and use objects without making them accessible outside of that class. Although nested classes can be convenient, they are also easy to overuse, which can make your class more difficult to work with.

Nested classes implicitly have at least the same access level as the containing class. For example, if the nested class is public but the containing class is internal, the nested class is implicitly internal as well, and only members of that assembly can access the nested class. However, if the containing class is public, the nested class follows the same accessibility rules as a non-nested class.

You should consider implementing a class as a nested class if it has no stand-alone significance and can be logically contained by another class or members of the class need to access private data of the containing class. Nested classes should generally not be public because they are for the internal use of the containing class.

  • + Share This
  • 🔖 Save To Your Account