其他分享
首页 > 其他分享> > Why use nested classes?

Why use nested classes?

作者:互联网

Why use nested classes in C# code?

Enable encapsulation

Many times nested classes are implementation details of the class. Users of the main class should not have to care about their existence. You should be able to change them at will without requiring the users of the main class to change their code.

Avoid name pollution

You should not add types, variables, functions, etc. in a scope unless they are appropriate in that scope. This is slighly different than encapsulation. It could be useful to expose the interface of a nested type but the proper place for the nested type is still the main class. In C++ land, iterator types are one such example.

标签:code,use,main,should,classes,Why,nested,class
来源: https://www.cnblogs.com/baron-li/p/16407506.html