Cloning a Collection

Posted on 2006-11-16
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)

Question by:fred2k3
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
LVL 15

Expert Comment

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.
LVL 15

Accepted Solution

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>();
                  ClonableList<int> list2 = (ClonableList<int>)list1.Clone();
                  foreach (int i in list2) {

LVL 37

Expert Comment

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 ;-)
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

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.

LVL 37

Expert Comment

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



Author Comment

ID: 17957903

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

729 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