Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Cloning a Collection

Posted on 2006-11-16
6
Medium Priority
?
3,906 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 1000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

971 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