Static classes in c#

前端 未结 8 710
别那么骄傲
别那么骄傲 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:11

    It isn't exactly equivalent. For example you can pass a reference to a singleton instance as an argument, which you can't do with a static class as there isn't an instance.

    What do you mean by "danger"?

    0 讨论(0)
  • 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<T>.Default / EqualityComparer<T>.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.

    0 讨论(0)
提交回复
热议问题