Is it possible to wrap a C# singleton in an interface?

后端 未结 4 887
挽巷
挽巷 2021-02-20 05:28

I currently have a class in which I only have static members and constants, however I\'d like to replace it with a singleton wrapped in an interface.

But how can I do th

4条回答
  •  萌比男神i
    2021-02-20 05:49

    Interfaces can not have instances in C#, I think you only need to:

    Implement the singleton pattern (yes, you'll need a static attribute or method to get the instance, but everything else does not require to be static)

    On the other hand, your singleton can implement an interface if you want, just remember that other classes can also implement that same interface

提交回复
热议问题