标签云

微信群

扫码加入我们

WeChat QR Code


As it stands I had to add "protected" to the constructor in order to compile.

2018年09月25日59分05秒

The question is difficult to read. Does "Is this Generic Singleton class good? Is it thread safe?" still convey the correct meaning?

2018年09月25日59分05秒

Feel free to do anything you see fit with this question, its community wiki

2018年09月25日59分05秒

One caveat with any Singleton based on an Instance property is that you have to be careful if your class constructor has side effects. If you hover over the Instance property in the debugger while in the middle of singleton construction (because you set a breakpoint in one of the side effects, for example), you can actually execute the constructor again and cause all sorts of fun mind-boggling problems. This happens because the debugger (by default) evaluates properties automatically when you hover over them.

2018年09月25日59分05秒

That a good post ... I amended the question to reflect these changes

2018年09月26日59分05秒

The code as posted is thread-safe thanks to volatile.

2018年09月25日59分05秒

You have the correct answer. This is the best implementation because it is the simplest, it's built into the language and you're not burning your base class to implement a trivial feature.

2018年09月25日59分05秒

wizlb By moving common logic to base class you take the responsibility to ensure that pattern is used correctly from authors of derived classes to single base class.

2018年09月25日59分05秒

Im sorry, im no longer comfortable with this being correct, the problem is that you end up having a type that is both newable and can be accessed via the singleton ...

2018年09月26日59分05秒

Just a quick question Sam: How did you manage to create a new instance of the class? According to the code above, the constructor is private...

2018年09月26日59分05秒

This pattern is good, but there's one thing that bothers me : the new() type constraint implies that the singleton has a public constructor, which breaks the singleton pattern. I think this constraints should be removed, and Activator.CreateInstance should be used to call the protected constructor

2018年09月26日59分05秒

T must have a public new()

2018年09月26日59分05秒

Darksider, Singleton<Dog> ... var d = new Dog() works just fine... which I would prefer did not. See: cade's answer.

2018年09月25日59分05秒

The class is not a singleton, because the public constructor allows to create multiple instances . The class is useful, but I've renamed it to SingleInstanceBase

2018年09月25日59分05秒

Please see question. This is the way of improving question class using .net 4

2018年09月25日59分05秒

This is really not an improvement, for one its not using generics so you have to cast Instance when you subclass, its also potentially initializing the object before you really need it.

2018年09月26日59分05秒

Actually it's not initializing before you need it; read the MSDN link provided; "the instance is created the first time any member of the class is referenced".

2018年09月26日59分05秒

Yerp adding volatile is a good point

2018年09月26日59分05秒

isn't volatile a c++ thing?

2018年09月25日59分05秒

See: msdn.microsoft.com/en-us/library/x13ttww7(VS.71).aspx

2018年09月25日59分05秒

If you replace '= default(T)' with '= new T()' then you don't have to do the double-check locking and just 'get { return _Instance; }'

2018年09月26日59分05秒

Wrong. There are quite a few situations where singletons are useful and static classes do not work eg. try making this static. public TimeSpan this[string filename] { get { return new TimeSpan(); } set { } }

2018年09月26日59分05秒

Also, you can't inherit static methods so you can't use inheritance to build your singleton class.

2018年09月26日59分05秒

I'll amend my comment... a static class is a GOOD solution for grouping static methods, but technically, it does not implement singleton since there is no "instance" and you don't get instance semantics like inheritance.

1970年01月01日00分03秒

The singleton could implement an interface or extend another class. That will work in static a static class with static methods.

2018年09月25日59分05秒