For example, T-shirts have DataTable, DataRow, DataColumn, which are designed as separate classes. Why did the shirts go this way and not make the types nested inside one another? After all, they are interrelated. Or does it contradict something?
1 answer
Internal classes are essentially no different from internal private methods.
The method is made internal because it does not make sense outside the context of the specific implementation of this class, so there is no point in using it outside. In the same way, the classes that are needed / interesting only as a detail of the implementation of the outer class should be hidden inside this class so that they are not visible to anyone and do not clutter the scope.
A canonical example is the implementation of various iterators and enumerators. Outside of the outer class, only the interface usually makes sense, so a specific class implementing IEnumerable<T> remains “behind the scenes”.
In the case of DataTable , DataRow and DataColumn working with each of them makes sense for us DataTable users. Therefore, these classes are not placed inside the DataTable , but lie outside.
In the case of an open inner class, the outer class is essentially a namespace. Technically, the need for such a class structure may arise due to the fact that inner classes have access to the closed fields of the enclosing classes, and thus represent an analogue of friend classes from C ++. However, such a need may mean an error in design: separate entities accessible to the outside world must communicate with each other through open interfaces.
The answer is based on this answer with en.SO