我目前正在学习C#,并学习构造函数和链式调用构造函数,以避免在每个构造函数中粘贴相同的代码(变量的相同值)。
我有三个构造函数,一个没有参数,一个有三个参数,另一个有四个参数。我想要做的是,使用默认构造函数调用具有三个参数的构造函数,传递参数(变量)的默认值,并且具有三个参数的构造函数应该调用具有四个参数的构造函数。我似乎已经解决了第一个问题,列出了默认值,但我不知道如何编写具有三个参数的构造函数,然后在需要时将其调用具有四个参数的构造函数。
默认构造函数应将类型为string的所有实例变量赋值为string.Empty。
public Address()
{
m_street = string.Empty;
m_city = string.Empty;
m_zipCode = string.Empty;
m_strErrMessage = string.Empty;
m_country = Countries;
}
public Address(string street, string city, string zip)
{
}
public Address(string street, string city, string zip, Countries country)
{
}
我想执行以下操作,但是它无法正常工作:-
public Address(string street, string city, string zip)
: this street, string.Empty, city, string.Empty, zip, string.Empty
{
}