Reputation: 9299
I study C# and I'm trying to understand the overloaded constructor, how it works and the point using them like a chain call? Why just not have only one constructor with all necessary parameters? Below I have some helping code for a task I'm working with, and I need some help to understand the point with all this constructors. Preciate some help! Thanks!
public class Email
{
//private email
private string m_personal;
//work mail
private string m_work;
public Email()
{
}
public Email(string workMail) : this(workMail, string.Empty)
{
}
public Email(string workMail, string personalMail)
{
m_work = workMail;
m_personal = personalMail;
}
public string Personal
{
//private mail
get { return m_personal; }
set { m_personal = value; }
}
public string Work
{
get { return m_work; }
set { m_work = value; }
}
public string GetToStringItemsHeadings
{
get { return string.Format("{0,-20} {1, -20}", "Office Email", "Private Email"); }
}
public override string ToString()
{
string strOut = string.Format("{0,-20} {1, -20}", m_work, m_personal);
return strOut;
}
}
Upvotes: 1
Views: 136
Reputation: 30837
This is not about Constructors
. All kind of overloaded functions simplify library usage. When we code a class, we are going to encapsulate all the complexities in a black box and this is possible with some known best Practices
. The good example is .NET
libraries which you can use easily (remember those overloaded functions/constructors).
This is not about right or wrong, we use overloaded functions to make usage simpler so there would be no need to pass null parameters when it is not needed.
Secondly we call the function with most parameters nested by the next function with less parameters to reduce Redundancy
. I mean avoiding copy/paste same code in all versions of function.
I think since the pattern is widely accepted and used, next generations of .NET
will hide this redundant versions in some way and we just code the function with the most parameters.
Upvotes: 2
Reputation: 2478
I would say that the second constructor is provided mainly for convenience purposes, to make the usage of the class easier in cases where there is no personal mail address. The user of the class then only needs to specify the working address, and the class itself will take care of the handling of the nonexistend personal address by setting a sensible default value for that field. If that parameter was missing, the user of the class would be made resposible for filling string.Empty for missing personal mail addresses.
The constructor is implemented as a call to another constructor to avoid duplication of code. It could also be written like
public Email(string workMail)
{
m_work = workMail;
m_personal = string.Empty;
}
but the existing implementation that calls the two-parameter constructor is the cleaner solution because it follows the "Don't repeat yourself" principle.
Upvotes: 1
Reputation: 13381
Check out:
http://blog.degree.no/2012/03/calling-an-overloaded-constructor-from-another-constructor-c/
Upvotes: 1
Reputation: 31454
Why just not have only one constructor with all necessary parameters?
What if users of your class are only interested in some parameters? Using your example, what if somebody doesn't have personal email? Should they pass null
or string.Empty
? This kind of type-level knowledge is best handled by type itself.
By exposing extra constructor with work email only, you're essentially telling your class consumers "Don't have personal email? Don't worry, I will handle it".
On top of that, chaining definitions naturally helps to avoid code redundancy.
Upvotes: 3
Reputation: 8352
The point is to avoid repeated code, as it will help you avoid this:
public Email(string workMail)
{
m_work = workMail;
m_personal = string.Empty;
}
public Email(string workMail, string personalMail)
{
m_work = workMail;
m_personal = personalMail;
}
Take in account that your constructor could do more than just assigning fields.
How it works? You can try it, debug it and learn it. I'm not sure but I think it will call first the overloaded one and then the code of your constructor. In you scenario, calling Email(string workMail) will call first Email(string workMail, string personalMail).
Upvotes: 1