8

I want to be able to instaniate a class with a public constructor that will by default call a private constructor and I think it is something close to the code below, but it isn't.

    public MySQLConnector()
        : this MySQLConnector (ConfigurationManager.AppSettings["DBConnection"])
    {
    }

    private MySQLConnector(string dbConnectionString)
    {
        //code
    }
naveen goyal
  • 4,571
  • 2
  • 16
  • 26
Recursor
  • 542
  • 5
  • 18
  • Same way as [calling any other constructor of the same type](http://stackoverflow.com/questions/4009013/call-one-constructor-from-another) .. – user2864740 Nov 14 '13 at 07:37

1 Answers1

9

You've almost got it. Just use this(...), without the class name:

public MySQLConnector()
    : this(ConfigurationManager.AppSettings["DBConnection"])
{
}

This is documented in Using Constructors (C# Programming Guide):

A constructor can invoke another constructor in the same object by using the this keyword. Like base, this can be used with or without parameters, and any parameters in the constructor are available as parameters to this, or as part of an expression.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331