Should I recommend sealing classes by default?

后端 未结 13 1246
青春惊慌失措
青春惊慌失措 2021-01-30 01:57

In a big project I work for, I am considering recommending other programmers to always seal their classes if they haven\'t considered how their classes should be subclassed. Oft

13条回答
  •  野性不改
    2021-01-30 02:55

    Frankly I think that classes not being sealed by default in c# is kind of weird and out of place with how the rest of the defaults work in the language.

    By default, classes are internal. By default fields are private. By default members are private.

    There seems to be a trend that points to least plausible access by default. It would stand to reason that a unsealed keyword should exits in c# instead of a sealed.

    Personally I'd rather classes were sealed by default. In most ocassions when someone writes a class, he is not designing it with subclassing in mind and all the complexities that come along with it. Designing for future subclassing should be a conscious act and therefore I'd rather you explicitly have to state it.

提交回复
热议问题