Write less code while creating constructor
Creating overloaded constructor has always been hidious to me. For refreshment, an overloaded constructor is writing a constructor as an existing one providing different number of parameters. It is hidious because I have to call the same fields those I need in my constructor again and again. Let's see an example. Let's say, I have a class name "Employee" with three private fields 1. _name, 2. _age and 3. _gender.

Old school 'Employee'

class Employee
    {
        private string _name;
        private int _age;
        private string _gender;        
    }

Now, if I want to create an object of my "Employee" class with user provided name, age and gender then I must have a constructor in my class definition that takes all three parameters.

public Employee(string name, int age, string gender)
        {
            _name = name;
            _age = age;
            _gender = gender;
        }

The problem

Okay, everything is fine so far. Now, if I want that, the user can create an instance of my "Employee" class just by providing the name. In this case, age and gender will get some default value and the instance will be created. Well, I can achieve the effect by creating an overload of the constructor that just takes one parameter and provide some default values for age and gender inside the constructor like the following snippet.

class Employee
    {
        private string _name;
        private int _age;
        private string _gender;

        protected const string _sex = "Male";        

        public Employee(string name)
        {
            _name = name;
            _age = int.MaxValue;
            _gender = _sex;
        }

        public Employee(string name, int age, string gender)
        {
            _name = name;
            _age = age;
            _gender = gender;
        }
    }

As you can see, in the overloaded constructor I have to call all the required fields again and assign the passed in value and some default values. Later let's say I decided to have another constructor that takes two parameters (name and age). In that case I to have go through creating another constructor and call the same fields again. 

Let's simplify

But, we can minimize both size and pain while creating overloaded constructor and its very simple. We can call one constructor while creating an overloaded constructor. It is called "Cyclic constructor". As we know one of our constructors takes three parameters, we can create the other two overloaded versions simply by using the "this" keyword that allows to call a constructor from another constructor.
 

class Employee
    {
        private string _name;
        private int _age;
        private string _gender;

        protected const string _sex = "Male";
        
        public Employee(string name)
            : this(name, int.MaxValue, _sex)
        {

        }
        
        public Employee(string name, int age)
            : this(name, age, _sex)
        {

        }
        
        public Employee(string name, int age, string gender)
        {
            _name = name;
            _age = age;
            _gender = gender;
        }
    }

In these new versions of the overloaded constructors, I just passed the incoming parameter(s) as the arguments of the constructor that takes three parameters. In the first overloaded constructor, user provides only the name. As the constructor I am calling uses three parameters, I am passing some default values for _age and _gender along with the user given name inside the ":this()". Same thing happens in the second constructor also. The user provides name and age, thereby I am passing a default value for _gender inside the ":this()".

Creating an instance is as same as before. User of this class wont see any diffrence while creating an instance. For example: 

var emp_02 = new Employee("Sarwar");
var emp_01 = new Employee("Jahan", 28);

I found this way of creating overloaded constructor is much easy to maintain and so less to write. Hope, you have learnt something new. Thanks.       

March 26, 2014
Jahan Sarwar