Click here to Skip to main content
Click here to Skip to main content

Constructor Chaining in C#

By , 20 Oct 2011
 

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:

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.

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)

About the Author

Manjuke Fernando
Technical Lead Air Liquide Industrial Services (Singapore)
Sri Lanka Sri Lanka
Member
I have been in software industry for more than 8 years. I have developed different type of software using different languages. Many of them are database related (both web & window based), SQL being as the back end most of the time. Up-to-date I have knowledge in languages such as C#, VB.Net, T-SQL, JAVA, VB6 & C++, making C# the most proficient of all. Also I have worked using different technologies like ASP.Net, SharePoint, Crystal Reports (But I really hate designing reports) & MS SQL Server and have involved in designing & developing software for major companies like FedEx, Softlogic Holdings, IronOne Technologies & Brandix. Currently I am working as a Tech Lead in Singapore.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionuse name arguments insteadmemberRonDsz25 Oct '11 - 15:23 
http://msdn.microsoft.com/en-us/library/dd264739.aspx
less and concise code if you are using c#4.0 +
QuestionNot much info on "base"memberRiz Thon24 Oct '11 - 14:41 
The article is quite short, so you could explain a bit more about the "base" keyword for constructors. What happen if you don't specify anything? What happen if you don't specify anything and the base parameterless constructor doesn't exist or is private? ...
GeneralLike !memberraananv24 Oct '11 - 11:51 
Tx. Smile | :)
GeneralGood one..memberPritesh Aryan21 Oct '11 - 19:52 
Nice one help full...but as per SledgeHammer01 say using string.empty will be good practice.
GeneralMy vote of 4memberSledgeHammer0120 Oct '11 - 16:54 
I get that you are showing constructor chaining, but it is horrible practice to set strings to "". Set them to null or String.Empty.

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 20 Oct 2011
Article Copyright 2011 by Manjuke Fernando
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid