Copy list of items to another list

Gopichandar

I am trying to copy the list of items from list1 to another list list2. I'm able to do that. However I don't want the changes made in list2 tobe reflected in list1.

class Program
{
    static void Main(string[] args)
    {
        List<MyClass> list1 = new List<MyClass>();
        list1.Add(new MyClass(){ID = 1, Name = "name1"});
        list1.Add(new MyClass(){ID = 2, Name = "name2"});
        list1.Add(new MyClass(){ID = 3, Name = "name3"});

        //Copy items from list1 to list2
        List<MyClass> list2 = new List<MyClass>(list1);

        list1.ForEach(x => Console.WriteLine(x.Name));   //It shows the name as added

        //Empty items in list2
        list2.ForEach(x => x.Name = string.Empty);

        //Print items in list1
        list1.ForEach(x => Console.WriteLine(x.Name));   //It shows the name is empty
        Console.ReadKey();
    }
}

class MyClass
{
    private int id;

    public int ID
    {
        get { return id; }
        set { id = value; }
    }

    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}

I hope there should be an easy way.

Tim Schmelter

Since this is a reference type you are changing all. You need to create a copy of that instance:

public class MyClass 
{
    private int id;

    public int ID
    {
        get { return id; }
        set { id = value; }
    }

    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public MyClass Copy()
    { 
        return new MyClass
        {
            ID = this.ID,
            Name = this.Name
        };
    }
}

Now you can create the second list in this way:

List<MyClass> list2 = new List<MyClass>(list1.Select(x => x.Copy()));

Of course you don't need that method. You could do that also on-the-fly in the LINQ query:

List<MyClass> list2 = new List<MyClass>(list1.Select(x => new MyClass { ID = x.ID, Name = x.Name }));

Another similar approach is a copy-constructor. A copy-constructor is used to initialize an instance by providing another instance of the same type:

public class MyClass
{
    private int id;

    public MyClass(MyClass instance)
    {
        this.id = instance.ID;
        this.name = instance.Name;
    }

    public int ID
    {
        get { return id; }
        set { id = value; }
    }

    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}

Now you could fill the list with copies in this way:

List<MyClass> list2 = new List<MyClass>(list1.Select(x => new MyClass(x)));

or with List.ConvertAll:

List<MyClass> list2 = list1.ConvertAll(x => new MyClass(x));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy all matching items from one list to another

From Dev

How to copy only new items from one list to another

From Dev

Excluding items from a list that exist in another list

From Dev

Sort a list by presence of items in another list

From Dev

transfer just items of a list to another list javascript

From Dev

How to create list of items from another list

From Dev

Remove list items within another list

From Dev

Copy a list of folder to another folder?

From Dev

Automapper not copy list object to another

From Dev

Can you copy a list in an element of another list?

From Dev

Copy list of strings to another list of strings

From Dev

Copy custom list object to another list object

From Dev

Copy list of of objects to another list of another objects within another object

From Dev

How to filter one list of items from another list of items?

From Dev

Comparing list with another list items and returning it if it has similar items

From Dev

get items of a list that matches items of another list in a for loop with Python

From Dev

Fastest method for copy list box Items to a RichTextBox

From Dev

Create a list with items from another list at indices specified in a third list

From Dev

Copy List to another List, but excluding certain properties of List type

From Dev

Adding the items of a list of sets to another set

From Dev

How to transfer items of one static list to another

From Dev

Remove list of items from another file in bash

From Dev

find lists that start with items from another list

From Dev

filtering a stream against items in another list

From Dev

Copy duplicates of one list to another using linq

From Dev

Copy certain characters from a list to another on prolog

From Dev

Copy certain characters from a list to another on prolog

From Java

Replace one list of items with another list in all possible combinations in python

From Dev

Delete specific items in a list based on numbers in another list in R

Related Related

  1. 1

    Copy all matching items from one list to another

  2. 2

    How to copy only new items from one list to another

  3. 3

    Excluding items from a list that exist in another list

  4. 4

    Sort a list by presence of items in another list

  5. 5

    transfer just items of a list to another list javascript

  6. 6

    How to create list of items from another list

  7. 7

    Remove list items within another list

  8. 8

    Copy a list of folder to another folder?

  9. 9

    Automapper not copy list object to another

  10. 10

    Can you copy a list in an element of another list?

  11. 11

    Copy list of strings to another list of strings

  12. 12

    Copy custom list object to another list object

  13. 13

    Copy list of of objects to another list of another objects within another object

  14. 14

    How to filter one list of items from another list of items?

  15. 15

    Comparing list with another list items and returning it if it has similar items

  16. 16

    get items of a list that matches items of another list in a for loop with Python

  17. 17

    Fastest method for copy list box Items to a RichTextBox

  18. 18

    Create a list with items from another list at indices specified in a third list

  19. 19

    Copy List to another List, but excluding certain properties of List type

  20. 20

    Adding the items of a list of sets to another set

  21. 21

    How to transfer items of one static list to another

  22. 22

    Remove list of items from another file in bash

  23. 23

    find lists that start with items from another list

  24. 24

    filtering a stream against items in another list

  25. 25

    Copy duplicates of one list to another using linq

  26. 26

    Copy certain characters from a list to another on prolog

  27. 27

    Copy certain characters from a list to another on prolog

  28. 28

    Replace one list of items with another list in all possible combinations in python

  29. 29

    Delete specific items in a list based on numbers in another list in R

HotTag

Archive