Nested Type

Why Trust Techopedia

What Does Nested Type Mean?

Nested type, in C#, is a type declared within an existing class or struct. Unlike a non-nested type, which is declared directly within a compilation unit or a namespace, nested type is defined within the scope of the containing (or outer) type.

Advertisements

Nested type is used only where its scope, visibility and lifetime ends within the containing type without exposing itself to other types. The enumerator member within a collection type is usually implemented as nested type to iterate over the collection. As an iterator, the enumerator enables the use of same client-side logic to iterate the collection, regardless of the data collection’s underlying structure.

In general, nested type is used only in situations where it logically belongs to the containing type. It is implemented when the containing type is entirely dependent on the nested type that helps to encapsulate the implementation details of the containing type. It is often used in scenarios where the containing type that has the nested type for its implementation is directly used without any requirement for instantiating its nested type.

Techopedia Explains Nested Type

A nested type can be accessed from anywhere within the program by declaring it with a public level of accessibility and using its fully qualified name.

For example, AddressInformation can be a nested type declared within Employee type, and can be accessed using its fully qualified name, Employee.AddressInformation.

Nested type has the following key properties:

  • It can have different forms of accessibility that include private, public, protected, protected internal and internal. By default, it has private accessibility.
  • It can access private and protected members of the containing type, including any inherited private and protected members. To access these members, an instance of the containing type has to be passed to the nested type through its constructor.
  • It permits multiple nesting levels, which allows the code block of one nested class to be defined within another.
  • It can inherit from the containing type and can also be inherited.
  • Private members of the nested type are invisible to the containing type.

Nested type should not be used when it has to be publicly exposed for instantiation by the client code or if it is referenced directly in the client code. It cannot be used in place of namespace for logical grouping constructs.

Advertisements

Related Terms

Margaret Rouse
Technology expert
Margaret Rouse
Technology expert

Margaret is an award-winning writer and educator known for her ability to explain complex technical topics to a non-technical business audience. Over the past twenty years, her IT definitions have been published by Que in an encyclopedia of technology terms and cited in articles in the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine, and Discovery Magazine. She joined Techopedia in 2011. Margaret’s idea of ​​a fun day is to help IT and business professionals to learn to speak each other’s highly specialized languages.