Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to use Collection.CopyTo?

How to use Collection.CopyTo?

Scheduled Pinned Locked Moved C#
questioncsharpdatabasedata-structurestutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nicknotyet
    wrote on last edited by
    #1

    .NET newbie question I have a Collection object that I would like to copy into another collection object. The collection is of type: System.Collections.ObjectModel.Collection The Collection.CopyTo method requires that I provide an array and an index. The question is: How do I syntactically express the array portion of the target collection. I can't do this, because param 1 is a Collection: Collection1.CopyTo(Collection2, 0) I hoped I could do something like this to specify the array, but alas no: Collection1.CopyTo(Collection2[], 0) Is there a way I can do this or do I have to enumerate the contents of Collection1 and Add to Collection2? Thanks

    S 1 Reply Last reply
    0
    • N nicknotyet

      .NET newbie question I have a Collection object that I would like to copy into another collection object. The collection is of type: System.Collections.ObjectModel.Collection The Collection.CopyTo method requires that I provide an array and an index. The question is: How do I syntactically express the array portion of the target collection. I can't do this, because param 1 is a Collection: Collection1.CopyTo(Collection2, 0) I hoped I could do something like this to specify the array, but alas no: Collection1.CopyTo(Collection2[], 0) Is there a way I can do this or do I have to enumerate the contents of Collection1 and Add to Collection2? Thanks

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Well, you could always do

      Collection2.Clear();
      Collection2.AddRange(Collection1);

      Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      N 1 Reply Last reply
      0
      • S S Senthil Kumar

        Well, you could always do

        Collection2.Clear();
        Collection2.AddRange(Collection1);

        Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        N Offline
        N Offline
        nicknotyet
        wrote on last edited by
        #3

        No such method (AddRange) on the Collection class.

        S 1 Reply Last reply
        0
        • N nicknotyet

          No such method (AddRange) on the Collection class.

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          My bad. I assumed you were using the List class. Looks like you have no other option other than looping over and copying elements.

          Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

          N 1 Reply Last reply
          0
          • S S Senthil Kumar

            My bad. I assumed you were using the List class. Looks like you have no other option other than looping over and copying elements.

            Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

            N Offline
            N Offline
            nicknotyet
            wrote on last edited by
            #5

            Such is life in .NET. Times like this I miss things like memcpy and void ** :)~

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups