Solved

Cloning a Collection

Posted on 2006-11-16
6
3,895 Views
Last Modified: 2012-05-05
Is there a command to shallow clone a collection (e.g. Collection<int>)?

I know you can manually copy the objects the one collection into the other, but is there a quicker way (i.e. a command)


Thanks
0
Comment
Question by:fred2k3
  • 2
  • 2
  • 2
6 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17956345
Collections will have a propected method MemberwiseClone() which is inherited from object.
If you create a class derived from a collection you could implement a Clone() method by exposing this.
Alternatviely use the CopyTo method of the ICollection interface or the ToArray() method.
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 250 total points
ID: 17956484
Quick example :

using System;
using System.Collections.Generic;

namespace ConsoleApplication1 {

      class ClonableList<T> : List<T>{

            public object Clone(){
                  return this.MemberwiseClone();
            }
      }

      class Program {
            static void Main(string[] args) {

                  ClonableList<int> list1 = new ClonableList<int>();
                  list1.Add(1);
                  list1.Add(2);
                  list1.Add(3);
                  list1.Add(4);
                  ClonableList<int> list2 = (ClonableList<int>)list1.Clone();
                  foreach (int i in list2) {
                        Console.WriteLine(i.ToString());
                  }
                  Console.ReadLine();

            }
      }
}
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 17956494
the collections also provide a copy constructor ...

Collection<int> test = new Collection<int>(SomeOtherCollection);

btw: int is probably a bad example since as it is a value type one could technically call this a deep copy ;-)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:fred2k3
ID: 17957774

To ozymandias thanks for the solution.

To Gregory You solution doesn't work since after performing the initialisation of test ,if you add an object to SomeOtherCollection the object is also added to the test Collecetion.

0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 17957846
whoops for collection<T> when passed an IList creates a wrapper .. list<T> does a shallow copy.

Cheers,

Greg
0
 

Author Comment

by:fred2k3
ID: 17957903
thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now