Click here to Skip to main content
15,122,354 members
Articles / Programming Languages / C#
Tip/Trick
Posted 15 Sep 2011

Tagged as

Stats

48.1K views
10 bookmarked

How to call an overloaded constructors in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
15 Sep 2011CPOL
A guide on how to call overload constructors in C#
Disclaimer: I wasn't really sure whether to post this or not as it is very baisc and I would exepect every programmer to know this, but after inheriting some code and reviewing it I came across this (names changed, but logic is the same)

C#
namespace Dummy
{
    public class Something
    {
        private string myName;
        private int myIndex;

        public Something()
        {
            myName = "default";
            myIndex = -1;

            DoStuff(); 
        }

        public Something(string name)
        {
            myName = name;
            myIndex= -1;

            DoStuff(); 
        }

        public Something(string name, index)
        {
            myName = name;
            myIndex= index;

            DoStuff(); 
        }

        private void DoStuff()
        {
            // logic
        }
                    
        // rest of class definition    
}


Whilst this isn't truly horrific, it is not best practice as it contains code duplication and can be replaced with:

C#
namespace Dummy
{
    public class Something
    {
        private string myName;
        private int myIndex;

        public Something() : this("default"){}

        public Something(string name) : this(name, -1) {}

        public Something(string name, index)
        {
            myName = name;
            myIndex= index;

            DoStuff(); 
        }

        private void DoStuff()
        {
            // logic
        }
                    
        // rest of class definition    
}

License

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

Share

About the Author

Reiss
Program Manager
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralRe: My bad, I read the constructor execution path the wrong way ... Pin
Doc Lobster20-Sep-11 23:12
MemberDoc Lobster20-Sep-11 23:12 
GeneralReason for my vote of 5 Thank you for this. Yes you would th... Pin
Kamran Behzad26-Sep-11 14:56
MemberKamran Behzad26-Sep-11 14:56 
GeneralReason for my vote of 1 Alternative 2 is much beater solutio... Pin
Milan Stanacev21-Sep-11 3:51
MemberMilan Stanacev21-Sep-11 3:51 
GeneralRe: Alternative 2 only works in .NET 4.0, and won't work with ei... Pin
Richard Deeming22-Sep-11 8:01
mveRichard Deeming22-Sep-11 8:01 
GeneralI think there is a mistake in the refactored example. Origin... Pin
Doc Lobster20-Sep-11 22:18
MemberDoc Lobster20-Sep-11 22:18 
GeneralRe: Hi Doc, regardless of which constructor you call in your imp... Pin
Reiss20-Sep-11 22:30
professionalReiss20-Sep-11 22:30 
Hi Doc, regardless of which constructor you call in your implementing code, the final constructor always gets called, so DoStuff() is always executed
GeneralI used to do constructors like the first example a lot when ... Pin
Steven Atkinson20-Sep-11 18:59
MemberSteven Atkinson20-Sep-11 18:59 
GeneralReason for my vote of 5 Obvious … after I worked through it.... Pin
Brendan Costigan20-Sep-11 5:52
MemberBrendan Costigan20-Sep-11 5:52 
GeneralVery nice I actually never knew this. Is this something tha... Pin
MrSmoofy19-Sep-11 9:51
MemberMrSmoofy19-Sep-11 9:51 
GeneralReason for my vote of 5 Obvious, but rarely used like it sho... Pin
fjdiewornncalwe15-Sep-11 9:23
professionalfjdiewornncalwe15-Sep-11 9:23 
GeneralReason for my vote of 5 It should be obvious, but as you men... Pin
Dr.Walt Fair, PE15-Sep-11 8:42
professionalDr.Walt Fair, PE15-Sep-11 8:42 

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.