Click here to Skip to main content
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 ... PinmemberDoc Lobster20-Sep-11 22:12 
GeneralReason for my vote of 5 Thank you for this. Yes you would th... PinmemberKamran Behzad26-Sep-11 13:56 
GeneralReason for my vote of 1 Alternative 2 is much beater solutio... PinmemberMilan Stanacev21-Sep-11 2:51 
Reason for my vote of 1
Alternative 2 is much beater solution.
GeneralRe: Alternative 2 only works in .NET 4.0, and won't work with ei... PinmemberRichard Deeming22-Sep-11 7:01 
GeneralI think there is a mistake in the refactored example. Origin... PinmemberDoc Lobster20-Sep-11 21:18 
GeneralRe: Hi Doc, regardless of which constructor you call in your imp... PinmemberReiss20-Sep-11 21:30 
GeneralI used to do constructors like the first example a lot when ... PinmemberSteven Atkinson20-Sep-11 17:59 
GeneralReason for my vote of 5 Obvious … after I worked through it.... Pinmemberdigbyd20-Sep-11 4:52 
GeneralVery nice I actually never knew this. Is this something tha... PinmemberMrSmoofy19-Sep-11 8:51 
GeneralReason for my vote of 5 Obvious, but rarely used like it sho... PinmemberMarcus Kramer15-Sep-11 8:23 
GeneralReason for my vote of 5 It should be obvious, but as you men... PinsubeditorWalt 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
Web04 | 2.8.141030.1 | Last Updated 15 Sep 2011
Article Copyright 2011 by Reiss
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid