c# Public Nested Classes or Better Option?

后端 未结 8 661
青春惊慌失措
青春惊慌失措 2020-12-03 05:49

I have a control circuit which has multiple settings and may have any number of sensors attached to it (each with it\'s own set of settings). These sensors may only be used

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 06:07

    While I feel Eric's answer is correct, it is important to realize it doesn't really fully address what your situation is.

    Your case sounds very similar to one I frequently find myself in where you have a class which is really implementation details of another class, however, some details or functionality of that sub-component naturally lend themselves towards being exposed directly for some minor aspects that are not governed by the parent.

    In these cases, what you can do is use interfaces. The nested classes need not be public as they really are internal details of the class they are nested within, but a subset of functionality (an interface) needs to be made publicly available and can be implemented by the class.

    This allows for construction of the internal structures to be controlled by the class they are nested within while still allowing direct access to the type from a namespace for external references. (The caller will use SomeNamespace.IChildApi as the name rather than SomeNamespace.NestingClass.NestedClass.SecondNestedClass.ThirdNestedClass, etc.)

提交回复
热议问题