C# Object Array CopyTo links both arrays' values?

Posted by Dutchie432 on Stack Overflow See other posts from Stack Overflow or by Dutchie432
Published on 2010-05-05T13:55:51Z Indexed on 2010/05/05 13:58 UTC
Read the original article Hit count: 133

Filed under:
|
|

Okay, I have what I think is a simple question.. or just a case of me being a C# beginner.

I have an array of custom objects (clsScriptItem) that I am populating from a database. Once the items are loaded, I want to back them up to "backup" array so I can revert the information back after changing the main array. However, when I use CopyTo to copy the array and then alter the original array, the backup array is also being altered... I though CopyTo merely copied values + structure from one array to another.

    private void backupItems()
    {
        lastSavedItems = new clsScriptItem[items.Length];

        items.CopyTo(lastSavedItems, 0);

        //items[0].nexts[0] is 2
        //lastSavedItems[0].nexts[0] is 2

        items[0].nexts[0] = "-1";

        //items[0].nexts[0] is -1
        //lastSavedItems[0].nexts[0] is also -1


    }

How do I backup this data without having the two arrays be 'linked'??

© Stack Overflow or respective owner

Related posts about c#4.0

Related posts about arrays