I’m trying to design a system of chaining constructors in a way that I think may not be possible, but I want to check first.
I’ve got 5 overloaded constructors as follows:
public SqlCeDB(string filename)
public SqlCeDb(string filename, Dictionary<string, string> options)
public SqlCeDB(string filename, string password)
public SqlCeDB(string filename, string password, Dictionary<string, string> options)
public SqlCeDB(Dictionary<string, string> options)
I want to have all the logic built into the last constructor and have the others pass control to it. Right now I have a working first constructor as follows:
public SqlCeDB(string filename)
: this(new Dictionary<string, string> { { "DataSource", filename } })
{
}
But is there a way to do this for the second? I want to create a new Dictionary as above, but I would also need to copy the options that are already in the existing Dictionary called “options”.
Sure – just call the constructor which creates a copy of an existing one:
Collection initializers can still call parameterized constructors as normal. Note that this will create a new dictionary though – it won’t just add the new entry to the existing one.