添加到ICollection
我目前正在编写一个C#项目,我需要对项目进行unit testing。 对于我需要进行unit testing的方法之一,我使用ICollection,它通常从列表框的选定项中填充。
当我为方法创建unit testing时,它会创建该行
ICollection icollection = null; //Initialise to an appropriate value
如何创建此ICollection的实例和项目集合?
ICollection
是一个接口,你无法直接实例化它。 您需要实例化一个实现ICollection
的类; 例如, List
。 此外, ICollection
接口没有Add
方法 – 您需要为此实现IList
或IList
东西。
例:
List
List list = new List(); list.Add(object1); list.Add(object2); // etc... ICollection collection = list; // further processing of collection here.
与一些评论相反, IList
确实实现了ICollection
,至少据我所知。
假设您将拥有一组字符串,那么代码将是:
ICollection test = new Collection (); test.Add("New Value");
我相信您需要先将ICollection
接口inheritance到新类中,然后才能使用它。
如何实现ICollection
你可以做的是创建一个实现ICollection的类型,然后在测试中使用它。 List或Collection可用于创建对象的实例。 我想另一个问题是列表框的项目是什么类型。 使用.Add(…)方法将项添加到List或Collection非常简单。
List list = new List (); list.Add(item_from_your_list_box); list.Add(item2_from_your_list_box);
您是否需要更具体地使用此系列?