在创建类时,如何填充IList?

时间:2012-05-08 09:46:08

标签: c# ilist

我有以下课程:

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();

它会创建一个包含10个CityViewModel条记录的CityDetail

1 个答案:

答案 0 :(得分:7)

您需要向CityViewModel对象添加构造函数:

//Add a constructor
public CityViewModel()
{
    //Populate the variable
    CityDetails = Enumerable.Repeat(new CityDetail(), 10).ToList();
}