C# – Move one arraylist data to another arraylist in C#

arraylistc++

How to move one Arraylist data to another arraylist. I have tried for many option but the output is in the form of array not arraylist

Best Solution

First - unless you are on .NET 1.1, you should a avoid ArrayList - prefer typed collections such as List<T>.

When you say "copy" - do you want to replace, append, or create new?

For append (using List<T>):

    List<int> foo = new List<int> { 1, 2, 3, 4, 5 };
    List<int> bar = new List<int> { 6, 7, 8, 9, 10 };
    foo.AddRange(bar);

To replace, add a foo.Clear(); before the AddRange. Of course, if you know the second list is long enough, you could loop on the indexer:

    for(int i = 0 ; i < bar.Count ; i++) {
        foo[i] = bar[i];
    }

To create new:

    List<int> bar = new List<int>(foo);