添加到ICollection [英] Add to an ICollection
问题描述
我当前正在编写一个C#项目,并且需要对该项目进行单元测试.对于我需要进行单元测试的一种方法,我使用了一个ICollection,该ICollection通常是从列表框的选定项中填充的.
I am currently writing a C# project and I need to do unit testing for the project. For one of the methods that I need to unit test I make use of an ICollection which is normally populated from the selected items of a list box.
当我为方法创建单元测试时,它会创建行
When I create a unit test for the method it creates the line
ICollection icollection = null; //Initialise to an appropriate value
如何创建此ICollection的实例和该集合的项目?
How can I create an instance of this ICollection and an item to the collection?
推荐答案
ICollection
是一个接口,您不能直接实例化它.您需要实例化一个实现ICollection
的类.例如List<T>
.另外, ICollection
接口没有Add
方法-为此,您将需要实现IList
或IList<T>
的东西.
ICollection
is an interface, you can't instantiate it directly. You'll need to instantiate a class that implements ICollection
; for example, List<T>
. Also, the ICollection
interface doesn't have an Add
method -- you'll need something that implements IList
or IList<T>
for that.
示例:
List<object> icollection = new List<object>();
icollection.Add("your item here");
这篇关于添加到ICollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!