【发布时间】:2014-04-16 13:10:24
【问题描述】:
我将 ICollection 定义为
public virtual ICollection<Attempt> Attempts { get; set; }
而Attempt被定义为
public class Attempt
{
public Guid Id { get; set; }
public string AttemptsMetaData { get; set; }
public DateTime Time { get; set; }
public bool Answered { get; set; }
public DateTime Disconnected { get; set; }
}
现在我想将 ICollection 传递给一个方法。
public void AddTask(ICollection<Attempt> attempts)
{
// Do something to the item in attempts.
}
现在我必须为此集合创建一个示例数据。我不确定如何。下面的代码可能是错误的。 我以为是
// test data
Guid id = new Guid();
string attempt = "test";
DateTime dt = DateTime.Now.AddDays(-1);
bool answered = true;
DateTime disconnected = DateTime.Now;
ICollection <Attempt> attempts= new List<Attempt>();
// try to add sample data to the list
【问题讨论】:
-
ICollection 是一个更抽象的接口。对于这些场景,更喜欢 IEnumerable。
-
创建 Attempt 实例,设置属性值,然后调用 attempsts.Add(您的实例在此处);
标签: c# icollection