24

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

How can I create an instance of this ICollection and an item to the collection?

jnm2
  • 7,960
  • 5
  • 61
  • 99
Boardy
  • 35,417
  • 104
  • 256
  • 447

5 Answers5

36

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.

Example:

List<object> icollection = new List<object>();
icollection.Add("your item here");
Donut
  • 110,061
  • 20
  • 134
  • 146
  • But List does not implement ICollection. Boardy asked for an ICollection. –  Oct 18 '11 at 14:27
  • @Hungary1234 `List` implements `IList`, which is a descendant of `ICollection`. So `List` is also an `ICollection`. – Donut Oct 18 '11 at 16:34
  • 3
    The way @Donut wrote the comment makes me feel QED is required at the end of it. – DFTR Feb 21 '13 at 20:06
  • 1
    @Donut Who told that " ICollection interface doesn't have an Add method". Did u see this link http://msdn.microsoft.com/en-us/library/63ywd54z.aspx – Atish Kumar Dipongkor Aug 02 '13 at 10:37
  • 7
    @AtishDipongkor That link refers to the `ICollection` interface, which is different than the `ICollection` interface. You are correct that `ICollection` contains an `Add()` method; however, `ICollection` does not: http://msdn.microsoft.com/en-us/library/system.collections.icollection.aspx – Donut Aug 02 '13 at 18:22
5
List<Object> list = new List<Object>();
list.Add(object1);
list.Add(object2);
// etc...

ICollection collection = list;
// further processing of collection here.

Contrary to some comments, IList<T> does implement ICollection, at least as far as I can tell.

Altair
  • 304
  • 2
  • 9
3

Let's say you will have a collection of strings, then the code will be:

ICollection<string> test = new Collection<string>();
test.Add("New Value");
Fischermaen
  • 12,238
  • 2
  • 39
  • 56
0

I believe you need to inherit the ICollection interface into a new class before you can use it.

How to implement ICollection

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
0

What you can do is create a type that implements ICollection and from there make use of it in your testing. A List or Collection would work for creating an instance of the object. I guess another question would be what type are the items of the list box. Adding items to the List or Collection is pretty trivial just using the .Add(...) method.

List<T> list = new List<T>();
list.Add(item_from_your_list_box);
list.Add(item2_from_your_list_box);

Is there something more specific you need to be doing with this collection?

ShelbyZ
  • 1,494
  • 1
  • 14
  • 32