Click here to Skip to main content
11,412,526 members (74,902 online)
Click here to Skip to main content

Tagged as

How to call an overloaded constructors in C#

, 15 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

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)

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 Lobster at 20-Sep-11 23:12
memberDoc Lobster20-Sep-11 23:12 
My bad, I read the constructor execution path the wrong way around. Its a bit confusing to me that the simpler (shorter in means of arguments) constructors call the the ones being more complex.
GeneralReason for my vote of 5 Thank you for this. Yes you would th... Pin
Kamran Behzad at 26-Sep-11 14:56
memberKamran Behzad26-Sep-11 14:56 
GeneralReason for my vote of 1 Alternative 2 is much beater solutio... Pin
Milan Stanacev at 21-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 Deeming at 22-Sep-11 8:01
memberRichard Deeming22-Sep-11 8:01 
GeneralI think there is a mistake in the refactored example. Origin... Pin
Doc Lobster at 20-Sep-11 22:18
memberDoc Lobster20-Sep-11 22:18 
GeneralRe: Hi Doc, regardless of which constructor you call in your imp... Pin
Reiss at 20-Sep-11 22:30
memberReiss20-Sep-11 22:30 
GeneralI used to do constructors like the first example a lot when ... Pin
Steven Atkinson at 20-Sep-11 18:59
memberSteven Atkinson20-Sep-11 18:59 
GeneralReason for my vote of 5 Obvious … after I worked through it.... Pin
digbyd at 20-Sep-11 5:52
memberdigbyd20-Sep-11 5:52 
GeneralVery nice I actually never knew this. Is this something tha... Pin
MrSmoofy at 19-Sep-11 9:51
memberMrSmoofy19-Sep-11 9:51 
GeneralReason for my vote of 5 Obvious, but rarely used like it sho... Pin
Marcus Kramer at 15-Sep-11 9:23
memberMarcus Kramer15-Sep-11 9:23 
GeneralReason for my vote of 5 It should be obvious, but as you men... Pin
Walt Fair, Jr. at 15-Sep-11 8:42
subeditorWalt Fair, Jr.15-Sep-11 8:42 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.1 | Last Updated 15 Sep 2011
Article Copyright 2011 by Reiss
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid