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

How to call an overloaded constructors in C#

By , 15 Sep 2011
 
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)
 
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:
 
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)

About the Author

Reiss
Architect
United Kingdom United Kingdom
Member
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: My bad, I read the constructor execution path the wrong way ...memberDoc Lobster20 Sep '11 - 22:12 
GeneralReason for my vote of 5 Thank you for this. Yes you would th...memberKamran Behzad26 Sep '11 - 13:56 
GeneralReason for my vote of 1 Alternative 2 is much beater solutio...memberMilan Stanacev21 Sep '11 - 2:51 
GeneralRe: Alternative 2 only works in .NET 4.0, and won't work with ei...memberRichard Deeming22 Sep '11 - 7:01 
GeneralI think there is a mistake in the refactored example. Origin...memberDoc Lobster20 Sep '11 - 21:18 
I think there is a mistake in the refactored example. Originally every constructor would call DoStuff(), in the revised version only the constructor Something(string name, index) would do this.
GeneralRe: Hi Doc, regardless of which constructor you call in your imp...memberReiss20 Sep '11 - 21:30 
GeneralI used to do constructors like the first example a lot when ...memberSteven Atkinson20 Sep '11 - 17:59 
GeneralReason for my vote of 5 Obvious … after I worked through it....memberdigbyd20 Sep '11 - 4:52 
GeneralVery nice I actually never knew this. Is this something tha...memberMrSmoofy19 Sep '11 - 8:51 
GeneralReason for my vote of 5 Obvious, but rarely used like it sho...memberMarcus Kramer15 Sep '11 - 8:23 
GeneralReason for my vote of 5 It should be obvious, but as you men...subeditorWalt Fair, Jr.15 Sep '11 - 7:42 

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 15 Sep 2011
Article Copyright 2011 by Reiss
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid