Concept Of Constructor Chaining In C#

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.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. //Sandip Patil Code Snippets  
  7. namespace ConstructorChaining {  
  8.     class myDemoClass {  
  9.         public myDemoClass() {  
  10.             Console.WriteLine("1");  
  11.         }  
  12.         public myDemoClass(int x): this() {  
  13.             Console.WriteLine("2");  
  14.         }  
  15.         public myDemoClass(int x, int y): this(10) {  
  16.             Console.WriteLine("3");  
  17.         }  
  18.     }  
  19.     class Program {  
  20.         static void Main(string[] args) {  
  21.             myDemoClass c = new myDemoClass(10, 20);  
  22.             Console.Read();  
  23.         }  
  24.     }  
  25. }   

Explanation

  • Let us see how to use this keyword after our constructor’s definition.

The line of code given below demonstrates it.

  1. public myDemoClass(int x, int y): this(10) {  
  2.     Console.WriteLine("3");  
  3. }   

This is going to invoke the constructor with one argument as a parameter.

Note the code line given below.
    1. 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, 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace ConstructorChainWithPramsElimination {  
  7.     public class myProperties {  
  8.         private int prop1;  
  9.         private string propName;  
  10.         public myProperties(int prop1) {  
  11.             this.prop1 = prop1;  
  12.             Console.WriteLine("" + prop1);  
  13.         }  
  14.         public myProperties(int prop1, string propName): this(prop1) {  
  15.             this.propName = propName;  
  16.             Console.WriteLine("" + propName);  
  17.         }  
  18.     }  
  19.     class Program {  
  20.         static void Main(string[] args) {  
  21.             myProperties obj = new myProperties(25, "Sandip");  
  22.             Console.Read();  
  23.         }  
  24.     }  
  25. }   

Explanation

Output of above program is,

25
Sandip

Below line of code indicates, 

  1. 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.