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?
Contrary to some comments,
IList<T>
does implementICollection
, at least as far as I can tell.I believe you need to inherit the
ICollection
interface into a new class before you can use it.How to implement ICollection
Let's say you will have a collection of strings, then the code will be:
ICollection
is an interface, you can't instantiate it directly. You'll need to instantiate a class that implementsICollection
; for example,List<T>
. Also, theICollection
interface doesn't have anAdd
method -- you'll need something that implementsIList
orIList<T>
for that.Example:
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.
Is there something more specific you need to be doing with this collection?