In .NET there are Constructors without parameters, Constructors with parameters, and you can, as you know, have multiple Constructors each of which has a unique parameter list.
At run-time the .NET compiler "decides" which Constructor to call by matching the parameter list of the invocation (the code that calls the Constructor) to the possible Constructors available.
So, what qualifies as a "unique" parameter list:
1. a difference in the Types in the parameter list: that's intuitive for most of us.
2. a difference in the position of the (same) Types in the parameter list: that's something you probably
don't want to exploit in your code; why have two Constructors like:
public Class1(int x, string y) {}
public Class1(string y, int x) {}
Just to allow two different orderings of using the same parameters ?
The special syntax of following the closing parenthesis of the parameter list of a Constructor with a colon and the keyword "this," optionally followed by a list of variables in parentheses, allows you to
redirect the flow-of-control of your creation of a Class (or Struct) to other Constructors you have defined.
public Class1() {}
public Class1(int x, string y) : this() {}
Is going to call the parameterless Class1 Constructor
before it executes any code inside itself.
Just yesterday, the CP member ProgramFox, in response to a QA question here, showed me that you can even redirect execution from the parameterless Constructor to a Constructor with parameters as a way of setting default values:
public Class1() : this(0, String.Empty)
public Class1(int x, string y)
If you wrote in your code Class1 newClass1 = new Class1();
Then the parameterless constructor would execute code in the Constructor with parameters
before it executed code in its own body. Notice that in the Constructor with parameters in this example, that we removed the expression ": this()" ... if we didn't remove that you would get a StackOverflow error because the two Constructors would keep calling themselves.
This example of the parameterless Constructor forcing the execution of a Constructor with parameters is, I would guess, quite rare.
Finally, as if you didn't have enough tools to use in creating Constructors for every purpose: you can use
optional parameters in Constructor parameter lists; for example:
public Class1(string y, int x, bool z1, bool z2, long l = 123456789, string s = "what ?"){}
Note that here, at the end of the parameter list are two parameters "l" and "s" that are each followed by an equal-sign and a default value.
Now you could call any of:
Class1 myClass1 = new Class1("wow", 1, true, true);
Class1 myClass1 = new Class1("wow", 1, true, true, 987654321, "whoa");
Class1 myClass1 = new Class1("wow", 1, true, true, "whoa");
Class1 myClass1 = new Class1(s: "wow", z2:false, l: 987654321, x:100, y:"where ?", z1: false);
All this "freedom" can be intoxicating, but, in practice, most of the time you probably will create multiple Constructors for a good reason: you need to get different sorts of data into the instance of the Class you create ... in different circumstances.
Making sure you understand the parameter order- and type- matching the .NET compiler does in order to determine which Constructor is invoked, and also understanding the use of the ": this(...)" directive are most important.