Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

How to call an overloaded constructors in C#

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
20 Sep 2011CPOL 11.4K   1   3
All three of the Something methods could be replaced with a single definition of the form:public Something(string name = "default", index = -1){ myName = name; myIndex= index; DoStuff();}

All three of the Something methods could be replaced with a single definition of the form:


C#
public Something(string name = "default", index = -1)
{
    myName = name;
    myIndex= index;

    DoStuff();
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions

 
GeneralAlso should be good to note, this was introduced in .NET 4 a... Pin
Tony D Great27-Sep-11 2:46
Tony D Great27-Sep-11 2:46 
GeneralIf you do this, you can no longer use Activator.CreateInstan... Pin
Richard Deeming22-Sep-11 7:02
mveRichard Deeming22-Sep-11 7:02 
GeneralHi Robert, in this scenario you are correct, but sometimes y... Pin
Reiss20-Sep-11 21:02
professionalReiss20-Sep-11 21:02 
Hi Robert, in this scenario you are correct, but sometimes you may still need overloaded constructors and so using defaults like you have may not always work, or you have to duplicate the default value in multiple places which introduces human error if you have to change the default value.

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.