Solved

Cloning a Collection

Posted on 2006-11-16
6
3,896 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

939 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

4 Experts available now in Live!

Get 1:1 Help Now