clone - shallow and deep copy?

Posted on 2006-05-19
Last Modified: 2008-03-17
how would I use clone on an arraylist?
is it just
arraylist al = new arraylist();
alcloned = al.clone();

Also if this is a cloned / shallow copy how do I perform a deep copy?

Question by:paulwhelan
    LVL 3

    Expert Comment

    using the copy...method

    Author Comment

    Ok I see I can do


    So what would be examples using the two of them?

    And is copyto 'deep' and clone 'shallow'?


    Author Comment

    Is it like this?

                ArrayList al2 = al.Clone();
                ArrayList al3 = new ArrayList();
    LVL 6

    Accepted Solution


    Clone just creates a shallow copy.  This means for every reference type variable stored in the arraylist, the new arraylist will simply have a reference to the same piece of information.  CopyTo takes the ArrayList and copies some or all of it to a one dimensional array.

    You can try iterating over the ArrayList and populating a new one with new versions of each object as you go along.  This would of course be a lot easier if you know the data types that are being stored in the ArrayList in the first place.

    There is no general deep copy functionality in .NET for ArrayList as far as I am aware.  In fact, for an ArrayList, the Clone method seems to be somewhat broken in this regard since implementing the IClonable interface (and hence Clone) is meant to (and I quote from MSDN) "to support cloning beyond that supplied by MemberwiseClone." which it clearly doesn't in this case just providing another shallow copy.

    Serialization idea taken from:

    If the classes inside the arraylist are serializable you can do the following: (adapted from the above website)

    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;

    ArrayList a = new ArrayList();
    ArrayList b = new ArrayList();

    .. // populate a here with value types and serializable classes

    MemoryStream ms = new MemoryStream();
    BinaryFormatter bf = new BinaryFormatter();

    bf.Serialize(ms, a);
    b = (ArrayList)bf.Deserialize(ms); // ArrayList b now has an independent copy of everything from a

    Hope this helps,

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    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

    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…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    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.

    737 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

    20 Experts available now in Live!

    Get 1:1 Help Now