About interface ICollection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony

    About interface ICollection

    Hello!

    I'm reading in book and it says the following.
    "ICollectio n 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


  • Marc Gravell

    #2
    Re: About interface ICollection

    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(cop y, 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

    Comment

    • Peter Duniho

      #3
      Re: About interface ICollection

      On Tue, 10 Jun 2008 00:24:57 -0700, Tony <johansson.ande rsson@telia.com >
      wrote:
      Hello!
      >
      I'm reading in book and it says the following.
      "ICollectio n 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.Cop yTo() 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

      Comment

      • Ignacio Machin ( .NET/ C# MVP )

        #4
        Re: About interface ICollection

        On Jun 10, 3:24 am, "Tony" <johansson.ande rs...@telia.com wrote:
        Hello!
        >
        I'm reading in book and it says the following.
        "ICollectio n 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.Cou nt does the counting part
        ICollection.Cop yTo does the copy part

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

        Comment

        Working...