Click here to Skip to main content
15,878,852 members
Articles / Programming Languages / C#
Tip/Trick

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 53.2K   10   11
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)


Written By
Program Manager
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: My bad, I read the constructor execution path the wrong way ... Pin
Doc Lobster20-Sep-11 22:12
Doc Lobster20-Sep-11 22:12 
GeneralReason for my vote of 5 Thank you for this. Yes you would th... Pin
Kamran Behzad26-Sep-11 13:56
Kamran Behzad26-Sep-11 13:56 
Reason for my vote of 5
Thank you for this. Yes you would think this is too basic but I am embarrassed to say how many times I have confused myself over this, looking through my past code with frustration to find how I actually did it last time.

I can see from the comments that others are just as confused! Vote = 5!
GeneralReason for my vote of 1 Alternative 2 is much beater solutio... Pin
Milan Stanacev21-Sep-11 2:51
Milan Stanacev21-Sep-11 2:51 
GeneralRe: Alternative 2 only works in .NET 4.0, and won't work with ei... Pin
Richard Deeming22-Sep-11 7:01
mveRichard Deeming22-Sep-11 7:01 
GeneralI think there is a mistake in the refactored example. Origin... Pin
Doc Lobster20-Sep-11 21:18
Doc Lobster20-Sep-11 21:18 
GeneralRe: Hi Doc, regardless of which constructor you call in your imp... Pin
Reiss20-Sep-11 21:30
professionalReiss20-Sep-11 21:30 
GeneralI used to do constructors like the first example a lot when ... Pin
Steven Atkinson20-Sep-11 17:59
Steven Atkinson20-Sep-11 17:59 
GeneralReason for my vote of 5 Obvious … after I worked through it.... Pin
Brendan Costigan20-Sep-11 4:52
Brendan Costigan20-Sep-11 4:52 
GeneralVery nice I actually never knew this. Is this something tha... Pin
MrSmoofy19-Sep-11 8:51
MrSmoofy19-Sep-11 8:51 
GeneralReason for my vote of 5 Obvious, but rarely used like it sho... Pin
fjdiewornncalwe15-Sep-11 8:23
professionalfjdiewornncalwe15-Sep-11 8:23 
GeneralReason for my vote of 5 It should be obvious, but as you men... Pin
Dr.Walt Fair, PE15-Sep-11 7:42
professionalDr.Walt Fair, PE15-Sep-11 7: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.