Static classes in c#

前端 未结 8 725
别那么骄傲
别那么骄傲 2021-01-18 03:09

In answering this question (https://stackoverflow.com/questions/352317/c-coding-question#352327), it got me wondering...

Is there any danger in regarding a static cl

8条回答
  •  失恋的感觉
    2021-01-18 04:13

    In many ways, a static class and a singleton are similar. One big difference is that a singleton might implement some interfaces, which isn't possible with a static class. For example, Comparer.Default / EqualityComparer.Default provide (via the interface) the ability to use the item in sorting / dictionary usage.

    It is also possible (though tricky) to use a singleton with the standard serialization frameworks. With a static class, you'd have to manage any state persistence manually.

提交回复
热议问题