我有一个小问题,我不明白为什么,它很容易绕过它,但我仍然想明白。C#3.0 - 对象初始化程序
我有以下类:
public class AccountStatement : IAccountStatement
{
public IList<IAccountStatementCharge> StatementCharges { get; set; }
public AccountStatement()
{
new AccountStatement(new Period(new NullDate().DateTime,newNullDate().DateTime), 0);
}
public AccountStatement(IPeriod period, int accountID)
{
StatementCharges = new List<IAccountStatementCharge>();
StartDate = new Date(period.PeriodStartDate);
EndDate = new Date(period.PeriodEndDate);
AccountID = accountID;
}
public void AddStatementCharge(IAccountStatementCharge charge)
{
StatementCharges.Add(charge);
}
}
(注STARTDATE,结束日期,帐户ID是自动的属性...)
如果我使用这种方式:
var accountStatement = new AccountStatement{
StartDate = new Date(2007, 1, 1),
EndDate = new Date(2007, 1, 31),
StartingBalance = 125.05m
};
当我尝试使用方法“AddStatementCharge:我结束了一个”null“StatementCha RGES列表...在一步一步,我清楚地看到我的列表获取值,但只要我退出德实例行,我的名单成为“空”
不能我的参数少构造函数调用另一个,我没有accountID,所以不能直接调用它。 必须使用:如前所述。或者简单地在无参数ctor中放置一个返回值。 谢谢! – pmlarocque 2008-09-24 15:33:39