创建类时如何填充 IList
本文关键字:填充 IList 何填充 创建 | 更新日期: 2023-09-27 18:33:33
我有以下类:
public class CityViewModel
{
public City City { get; set; }
public IList<CityDetail> CityDetails { get; set; }
public class CityDetail()
{
public CityDetail() {
Text = new HtmlText();
ImageFile = String.Empty;
Correct = false;
Explanation = new HtmlText();
}
public bool Correct { get; set; }
public HtmlText Text { get; set; }
public string ImageFile { get; set; }
public HtmlText Explanation { get; set; }
}
当我做这样的事情时,我怎样才能做到:var model = new CityViewModel();
它创建了一个包含十条CityDetail
记录的CityViewModel
?
您需要向 CityViewModel 对象添加一个构造函数:
//Add a constructor
public CityViewModel()
{
//Populate the variable
CityDetails = Enumerable.Repeat(new CityDetail(), 10).ToList();
}