Can singleton class be static?

2019-04-14 06:25发布

Can singleton class be static?

5条回答
▲ chillily
2楼-- · 2019-04-14 06:45

No singleton cannot be static

查看更多
劳资没心,怎么记你
3楼-- · 2019-04-14 06:47

(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.

查看更多
Melony?
4楼-- · 2019-04-14 06:50

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

查看更多
萌系小妹纸
5楼-- · 2019-04-14 06:51

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.

查看更多
再贱就再见
6楼-- · 2019-04-14 07:04

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.

查看更多
登录 后发表回答