Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#

Constructor Chaining in C#

Rate me:
Please Sign up or sign in to vote.
4.46/5 (17 votes)
20 Oct 2011CPOL 158.5K   9   10
Constructor Chaining is an approach where a constructor calls another constructor in the same or base class.

What is Constructor Chaining?

Constructor Chaining is an approach where a constructor calls another constructor in the same or base class.

This is very handy when we have a class that defines multiple constructors. Assume we are developing a class Student. And this class has three constructors. On each constructer, we have to validate the student's ID and categorize him/her. So if we do not use the constructor chaining approach, it would be something similar to what is shown below:

271582/screen_01_thumb_2_.png

Even though the above approach solves our problem, it duplicates code. (We are assigning a value to ‘_id’ in all our constructors). This is where constructor chaining is very useful. It will eliminate this problem. This time, we only assign values in one constructor which consists of the most number of parameters. And we call that constructor when the other two constructers are called.

C#
class Student {
    string _studentType = "";
    string _id = "";
    string _fName = "";
    string _lName = "";

    public Student(string id)
        : this(id, "", "") {

    }

    public Student(string id, string fName)
        : this(id, fName, "") {

    }

    public Student(string id, string fName, string lName) {
        //Validate logic.....
        _studentType = "<student_type>";

        _id = id;
        _fName = fName;
        _lName = lName;
    }
}

**Please note: If you do not specify anything [in this example, we used ‘this’), it will be considered that we are calling the constructor on the base class. And it’s similar to using ‘: base(…)’].

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Air Liquide Industrial Services (Singapore)
Singapore Singapore
My passion lies in building business intelligence and data-based solutions, writing about things I work with and talking about it. New technologies relevant to my line of work interest me and I am often seen playing with early releases of such technologies.

My current role involves architecting and building a variety of data solutions, providing database maintenance and administration support, building the organization’s data practice, and training and mentoring peers.

My aspiration over the next several years is to achieve higher competency and recognition in the field of Data Analytics and move into a career of data science.


Specialities: SQL Server, T-SQL Development, SQL Server Administration, SSRS, SSIS, C#, ASP.Net, Crystal Reports

Comments and Discussions

 
Questionconstractor chaining Pin
Member 144791614-Aug-19 22:22
Member 144791614-Aug-19 22:22 
Questionconstructor chaining Pin
Ahmed M Tawfik 28-May-16 1:36
Ahmed M Tawfik 28-May-16 1:36 
GeneralMy vote of 4 Pin
Iakovos Karakizas22-Jan-14 22:01
professionalIakovos Karakizas22-Jan-14 22:01 
Questionuse name arguments instead Pin
RonDsz25-Oct-11 15:23
RonDsz25-Oct-11 15:23 
QuestionNot much info on "base" Pin
Riz Thon24-Oct-11 14:41
Riz Thon24-Oct-11 14:41 
GeneralLike ! Pin
raananv24-Oct-11 11:51
raananv24-Oct-11 11:51 
GeneralGood one.. Pin
Pritesh Aryan21-Oct-11 19:52
Pritesh Aryan21-Oct-11 19:52 
GeneralRe: Good one.. Pin
Michael Freidgeim10-Apr-16 14:48
Michael Freidgeim10-Apr-16 14:48 
GeneralMy vote of 4 Pin
SledgeHammer0120-Oct-11 16:54
SledgeHammer0120-Oct-11 16:54 
GeneralRe: My vote of 4 Pin
Michael Freidgeim10-Apr-16 14:48
Michael Freidgeim10-Apr-16 14:48 
Disagree about string.Empty. See Use whatever you and your team find the most readable. - Stack Overflow[^]
Michael Freidgeim.
Blog: http://geekswithblogs.net/mnf/

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.