Now, we will learn the concept of constructor chaining in C# and .NET with the code snippets.
Let us start
A few important points need to be remembered here about constructor chaining, which are given below.
- It’s used when we want to invoke one constructor from another constructor.
- We need to use this keyword after our constructor’s definition.
Let us see in detail with the code snippet given below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //Sandip Patil Code Snippets
- namespace ConstructorChaining {
- class myDemoClass {
- public myDemoClass() {
- Console.WriteLine("1");
- }
- public myDemoClass(int x): this() {
- Console.WriteLine("2");
- }
- public myDemoClass(int x, int y): this(10) {
- Console.WriteLine("3");
- }
- }
- class Program {
- static void Main(string[] args) {
- myDemoClass c = new myDemoClass(10, 20);
- Console.Read();
- }
- }
- }
Explanation
- Let us see how to use this keyword after our constructor’s definition.
The line of code given below demonstrates it.
- public myDemoClass(int x, int y): this(10) {
- Console.WriteLine("3");
- }
This is going to invoke the constructor with one argument as a parameter.
- myDemoClass c = new myDemoClass(10, 20);
Here, actually we are invoking a constructor with the two arguments as the parameters inside which we invoked with one argument as a parameter. Again, finally we invoked the constructor with no parameter.
Output of the above program is given below.
1
2
3
Now, Let us see the main goal or purpose of using constructor chaining.
Main purpose is to eliminate the assignment of duplication of properties in same class.
Let’s see with code snippets,
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConstructorChainWithPramsElimination {
- public class myProperties {
- private int prop1;
- private string propName;
- public myProperties(int prop1) {
- this.prop1 = prop1;
- Console.WriteLine("" + prop1);
- }
- public myProperties(int prop1, string propName): this(prop1) {
- this.propName = propName;
- Console.WriteLine("" + propName);
- }
- }
- class Program {
- static void Main(string[] args) {
- myProperties obj = new myProperties(25, "Sandip");
- Console.Read();
- }
- }
- }
Explanation
Output of above program is,
25
Sandip
Below line of code indicates,
- myProperties obj = new myProperties(25, "Sandip");
We invoked constructor with two parameters and those values are initialized in different constructors with chaining and printed those values
One thing is need to be observe over here is,
We eliminated the repetitively assignment of prop1 Property of class myProperties
That is clearly indicating how we avoided assignment of duplicating properties in same class.

Sandip G PatilPosted Jan 26, 2017, 12:10 PM
Thanks a lot........................
Toti BirdPosted Jan 26, 2017, 11:18 AM
Very nice information, thanks for sharing the knowledge
Sandip G PatilPosted Jan 26, 2017, 6:49 AM
Thanks and welcome.................
Manav PandyaPosted Jan 26, 2017, 4:45 AM
Thanks for sharing sir ...
Sandip G PatilPosted Jan 25, 2017, 9:04 AM
Thanks a lot for comment.............................................................
Bikesh SrivastavaPosted Jan 25, 2017, 8:34 AM
Very nice article ,well explained.
Sandip G PatilPosted Jan 24, 2017, 1:31 AM
Surely i will do it..........Thanks a lot for comment
Nilesh ShahPosted Jan 23, 2017, 9:57 PM
good example code snippet, but I think it would have been even better if you also included in your post that why and in which case one should use this