Click here to Skip to main content
11,631,534 members (71,038 online)
Click here to Skip to main content

Tagged as

How to call an overloaded constructors in C#

, 15 Sep 2011 CPOL 28.9K 10
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

You may also be interested in...

Comments and Discussions

 
GeneralRe: My bad, I read the constructor execution path the wrong way ... Pin
Doc Lobster20-Sep-11 22:12
memberDoc 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
memberKamran Behzad26-Sep-11 13:56 
GeneralReason for my vote of 1 Alternative 2 is much beater solutio... Pin
Milan Stanacev21-Sep-11 2:51
memberMilan 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
memberRichard Deeming22-Sep-11 7:01 
GeneralI think there is a mistake in the refactored example. Origin... Pin
Doc Lobster20-Sep-11 21:18
memberDoc Lobster20-Sep-11 21:18 
GeneralRe: Hi Doc, regardless of which constructor you call in your imp... Pin
Reiss20-Sep-11 21:30
memberReiss20-Sep-11 21:30 
GeneralI used to do constructors like the first example a lot when ... Pin
Steven Atkinson20-Sep-11 17:59
memberSteven Atkinson20-Sep-11 17:59 
GeneralReason for my vote of 5 Obvious … after I worked through it.... Pin
digbyd20-Sep-11 4:52
memberdigbyd20-Sep-11 4:52 
GeneralVery nice I actually never knew this. Is this something tha... Pin
MrSmoofy19-Sep-11 8:51
memberMrSmoofy19-Sep-11 8:51 
Very nice I actually never knew this. Is this something that has always been in .NET or something added in later?
GeneralReason for my vote of 5 Obvious, but rarely used like it sho... Pin
Marcus Kramer15-Sep-11 8:23
memberMarcus Kramer15-Sep-11 8:23 
GeneralReason for my vote of 5 It should be obvious, but as you men... Pin
Walt Fair, Jr.15-Sep-11 7:42
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   

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
Web02 | 2.8.150723.1 | Last Updated 15 Sep 2011
Article Copyright 2011 by Reiss
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid