Private nested static class - Good or bad practice?

后端 未结 6 559
[愿得一人]
[愿得一人] 2021-01-04 03:20

Would it be considered a bad practice to nest a private static class inside of a non-static class?

public class Outer
{
    private static class Inner
    {
         


        
6条回答
  •  借酒劲吻你
    2021-01-04 03:48

    Both approaches are entirely valid.

    I wish developers would use private nested classes more often. In conjunction with c#'s partial keyword, it makes writing very complex classes much more maintainable. Imagine needing to build a class that has the complexity of a small application - much easier when you actually can build an entire private application, with classes that are totally internal to your complex outer class!

    One very common case I've seen is enumerables - these can be quite complex, especially when you start building custom iterators that can be chained, like LINQ. Hiding the complexity inside the individual classes is the very definition of encapsulation.

提交回复
热议问题