About interface ICollection


T

Tony

Hello!

I'm reading in book and it says the following.
"ICollection provides the ability to obtain the number of items in a
collection and to copy items into
a simple array type(inherits from IEnumerable)."

I have looked in interface ICollection and IEnumerable and there is no
method or property that
match in any way the last part of the statement from the book that says
"copy items into
a simple array type"

//Tony
 
Ad

Advertisements

M

Marc Gravell

Well, I'm not sure why it mentions "inherits from IEnumerable", since
that doesn't add anything here - but the answer is CopyTo:

ICollection data = new int[] { 1, 2, 3, 4, 5 };
int[] copy = new int[data.Count];
data.CopyTo(copy, 0);

Of course, in .NET 2.0 and above it would be preferable to use the
genric (typed) interfaces:

ICollection<int> data ...

This makes it more explicit that it expects CopyTo to operate on an
int[], rather than just an Array - and a lot of other things.

Marc
 
Ad

Advertisements

I

Ignacio Machin ( .NET/ C# MVP )

Hello!

I'm reading in book and it says the following.
"ICollection provides the ability to obtain the number of items in a
collection and to copy items into
a simple array type(inherits from IEnumerable)."
ICollection.Count does the counting part
ICollection.CopyTo does the copy part

I do not what CopyTo has to do with IEnumerable though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top