0

It looks like this overloaded constructor has itself as an interface, to create a Singleton Pattern - is that what is happening?

Is this a common constructor idiom in C#?

class clDBaccess
{

  // private field
  private readonly string conn;


  public clDBaccess()
    : this(ConfigurationManager.ConnectionStrings["foo"].ConnectionString)
  {
  }
  public clDBaccess(string connectionString)
  {
    this.conn = connectionString;
  }
  ...
  ...
whytheq
  • 34,466
  • 65
  • 172
  • 267
  • so it looks like, according to the second snippet of the accepted answer here http://stackoverflow.com/questions/17034475/in-c-sharp-what-category-does-the-colon-fall-into-and-what-does-it-really that the colon _isn't_ informing the compiler that there is inheritance or an interface, but simply saying run the other constructor. Seeing it in that other accepted answer seems to imply that yes, _it is an idiom_. – whytheq Mar 29 '16 at 22:02

5 Answers5

8

Singleton pattern has the constructor being called once ever. This is just method overloading to provide good defaults to a parameterless constructor.

You can call the constructor and create as many instances as you'd like, so it's not a singleton.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
6

No. The default constructor here is simply a convenience for using the connection string foo from app.config. There's nothing preventing multiple instances of this class from being created, but if you create them using the default constructor they'll connect to the same database (but won't necessarily be sharing the same connection).

One reason to do this is that the conn property is readonly - it must be initialized by the constructor (and cannot be changed after the constructor is done), and so the default constructor attempts to initialize it from a meanigful setting rather than setting it to null or string.Empty.

Dan Field
  • 20,885
  • 5
  • 55
  • 71
6

No, and any proper singleton should have a protected constructor.

using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
}
Eric
  • 462
  • 1
  • 5
  • 13
  • 3
    I find it amusing that you mention the necessity of a "protected constructor" and then use `private constructor`. – zzzzBov Mar 29 '16 at 18:12
  • 4
    Private is just more protected than protected. Both are still protected. – Eric Mar 29 '16 at 18:12
  • 1
    use this private static volatile Singleton instance; as mentioned in this https://msdn.microsoft.com/en-us/library/ff650316.aspx adn this link https://msdn.microsoft.com/en-us/library/x13ttww7.aspx – rashfmnb Mar 29 '16 at 18:12
  • 1
    @Eric, I'm well aware of the terminology, I just found it amusing. – zzzzBov Mar 29 '16 at 18:13
  • 1
    Singletons are usually a source of resource leaks anyhow, most of the time they are necessary but should be used with great care. – Eric Mar 29 '16 at 18:15
  • @rashfmnb nice reference - I recently listened to an interview with Eric Gamma - I'm pretty sure either he, or the interviewer, mentioned that if the gof book was to be updated and re-published Singleton might be excluded. – whytheq Mar 29 '16 at 19:58
3

"Singleton" means that there can only ever be one instance of a class. Typically you will find a public static property that returns the single instance of that class.

To ensure that the class can not be instantiated more than once, the constructor(s) will typically be private.

This is not the case here. It's a normal class with two constructors, where the parameterless constructor "redirects" to the preferred constructor with a useful default value.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
1

No, it does not fit the singleton pattern. The constructors would need to be private so that object instantiation is controlled by the class itself.

For example:

public class Singleton  
{
    private Singleton()
    {
    }
    private static Singleton instance = new Singleton();
    public static Singleton Instance
    {
        get
        {
            return instance;
        }
    }
}
Stringfellow
  • 2,788
  • 2
  • 21
  • 36