【发布时间】:2018-01-11 11:16:15
【问题描述】:
所以我需要了解哪种方式是正确/最好的?
第一种方式:
class Customer
{
string firstName;
string lastName;
public Customer(string firstName="non", string lastName="applicable")
{
this.firstName = firstName;
this.lastName = lastName;
}
}
第二种方式:
class Customer
{
string firstName;
string lastName;
public Customer()
: this("non","applicable")
{ }
public Customer(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
}
如果我正在创建一个方法 printFullName(),它在这两种情况下都有效。那么选择哪一个呢?第一个对我来说似乎更容易。
【问题讨论】:
标签: c# parameters constructor default