添加到ICollection [英] Add to an ICollection

查看:72
本文介绍了添加到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方法-为此,您将需要实现IListIList<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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆