471,318 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,318 software developers and data experts.

About interface ICollection

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
Jun 27 '08 #1
3 1932
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<intdata ...

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
Jun 27 '08 #2
On Tue, 10 Jun 2008 00:24:57 -0700, Tony <jo*****************@telia.com>
wrote:
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"
As has been the case with every other question you've posted of this
nature, it is impossible to provide any real insight as to what the text
might mean, given that we are not in possession of the text and have no
context.

That said, it seems to me that ICollection.CopyTo() is a method that meets
the book's description exactly, at least as far as is possible to tell
from what little context we do have. :)

Pete
Jun 27 '08 #3
On Jun 10, 3:24*am, "Tony" <johansson.anders...@telia.comwrote:
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.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jimski | last post: by
21 posts views Thread by Helge Jensen | last post: by
3 posts views Thread by Rafael Tejera | last post: by
1 post views Thread by =?Utf-8?B?aGVyYmVydA==?= | last post: by
1 post views Thread by Larry | last post: by
6 posts views Thread by Mike P | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.