Why seal a class?

前端 未结 10 1936
醉酒成梦
醉酒成梦 2020-11-28 04:51

I\'d like to hear what is the motivation behind the bulk of sealed classes in the .Net framework. What is the benefit of sealing a class? I cannot fathom how not allowing in

10条回答
  •  猫巷女王i
    2020-11-28 05:47

    A further consideration is that sealed classes can't be stubbed in your unit tests. From Microsoft's documentation:

    Sealed classes or static methods can't be stubbed because stub types rely on virtual method dispatch. For such cases, use shim types as described in Using shims to isolate your application from other assemblies for unit testing

提交回复
热议问题