C # Singleton GetInstance Method or Instance Property?

From an end-user perspective, an API that needs to “get instance of the “Singleton class, do you prefer to" get " a . Instance property or" call "of the .GetInstance () method?

public class Bar { private Bar() { } // Do you prefer a Property? public static Bar Instance { get { return new Bar(); } } // or, a Method? public static Bar GetInstance() { return new Bar(); } } 
+6
c # singleton instance
source share
7 answers

In C #, I would prefer .Instance as it follows the general guidelines.

+14
source share

If you want to create a singleton, you cannot just return a new object for every call to GetInstance or Instance getter. You should do something like this:

 public sealed class Bar { private Bar() { } // this will be initialized only once private static Bar instance = new Bar(); // Do you prefer a Property? public static Bar Instance { get { return instance; } } // or, a Method? public static Bar GetInstance() { return instance; } } 

And it doesn’t matter which way to do what you choose. If you prefer to work with properties, select it, if you prefer methods, this will be fine too.

+13
source share

As with everything, it depends :)

If singleton has lazy loading and represents something more than a trivial amount of work to create an instance, then GetInstance () is more suitable, since a method call indicates that the work is in progress.

If we simply mask the protection of a singleton instance, a property is preferable.

+4
source share

It depends. Do you need to pass parameters? If so, I would do GetInstance (). If not, it probably doesn’t matter (at least from the point of view of the call, since they are both methods in any case, however it matters if you try to be more standard, in which case an instance appears to be better) .

+3
source share
 public class Singleton { private volatile static Singleton uniqueInstance; private static readonly object padlock = new object(); private Singleton() { } public static Singleton getInstance() { if (uniqueInstance == null) { lock (padlock) { if (uniqueInstance == null) { uniqueInstance = new Singleton(); } } } return uniqueInstance; } } 

The above code implements a double check, first it checks to see if an instance is created, and if the lock is not set. After that in this block

  if (uniqueInstance == null) { uniqueInstance = new Singleton(); } 

if the instance is null then create it.

In addition, the uniqueInstance variable is declared mutable to ensure that the assignment of the instance variable is completed before access to the instance variable is available.

+2
source share

I prefer the property, these are standard templates.

0
source share

As @Rex said, it depends on the semantics you want to convey.

GetInstance () does not necessarily imply a singleton instance. Thus, I would use GetInstance () in the case where an instance is created on demand, direct new is not desirable, and the instance can be, but is not guaranteed to be the same. Pools of objects also meet these criteria. (Actually, singleton is a stateful specialization of a pool of objects :-))

The Static Instance property, on the other hand, means a singleton and saved instance of an instance.

Btw, as @RaYell mentioned that your code sample is not single, so you should not use the instance property. You can still use the GetInstance () method in this case, since it will serve as an instance of the factory.

0
source share

All Articles