Can singleton class be static?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 22:39:49

问题


Can singleton class be static?


回答1:


No. A singleton class is meant to be instantiated, because the term itself refers to an instance; if you make it a static class, you can't create a singleton object out of it.




回答2:


(Cat, meet pigeons.)

Yes, but only in practice, not in theory.

A singleton is a class that can only be instantiated once. A static class cannot be instantiated, so it cannot be called a singleton.

However, since we're talking about C#, static classes have constructors, so it is in effect being instantiated, and there can only ever be one instance so that to me looks a lot like a singleton.




回答3:


No. Singleton referes to single instance of the class. Static class does not have instances.




回答4:


A singleton is by definition an instance, so no.

But, you could have a static class where the methods access a private static variable. But that is just pushing the singleton a level deeper.




回答5:


No singleton cannot be static



来源:https://stackoverflow.com/questions/3565482/can-singleton-class-be-static

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!