Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cloning a Collection

Posted on 2006-11-16
6
Medium Priority
?
3,905 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
[X]
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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

670 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