Why does copy of the List still change properties

2019-02-27 13:15发布

Lets say I have this class

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool isActive { get; set; }
}

And use it like this:

    List<Employee> Employees = new List<Employee>();
    Employees.Add(new Employee { FirstName = "firstname", LastName = "lastname", isActive = true });
    List<Employee> EmployeesCopy = new List<Employee>(Employees);
    EmployeesCopy[0].isActive = false;

Why does change in isActive property of EmployeesCopy also modify property in the original list?

6条回答
一纸荒年 Trace。
2楼-- · 2019-02-27 13:16

You're doing a shallow copy, not a deep copy. This means that the new list contains the same objects from the original list.

To do a deep copy, you will need to iterate through your original list and make new Employee objects for the new list, like this.

private List<Employee> CloneEmployees(List<Employee> original)
{
    var newList = new List<Employee>();
    foreach (var employee in original)
    {
        newList.Add(new Employee 
            { 
                FirstName = employee.FirstName, 
                LastName = employee.LastName, 
                isActive = employee.isActive 
            });
    }
    return newList;
}
查看更多
一夜七次
3楼-- · 2019-02-27 13:18

Why does change in the isActive property of EmployeesCopy also modify the original list?|

Because both the list point to the same instance of the Employee object. You need to deep copy your Employee object as well.

查看更多
叼着烟拽天下
4楼-- · 2019-02-27 13:21

because by using new List<Employee>(Employees); will give you the new instance of List but not the objects contained in the list. You should consider cloning of objects contained in the list also , use Binary Serialization to serialize object graph.

查看更多
Lonely孤独者°
5楼-- · 2019-02-27 13:26

Because the new list still contains references to the same employee objects. You can create new ones in a new list by doing something like this:

    List<Employee> Employees = new List<Employee>();
    Employees.Add(new Employee { FirstName = "firstname", LastName = "lastname", isActive = true });
    List<Employee> EmployeesCopy = Employees.Select(x => new Employee(x)).ToList();

    public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public bool isActive { get; set; }

        public Employee()
        { }

        public Employee(Employee e)
        {
            FirstName = e.FirstName;
            LastName = e.LastName;
            isActive = e.isActive;
        }
    }
查看更多
forever°为你锁心
6楼-- · 2019-02-27 13:28

The copy you create is just a copy of the list. Not a copy of the objects. In other words, Employees[0] == EmployeesCopy[0].

查看更多
男人必须洒脱
7楼-- · 2019-02-27 13:29

The copy is a new List object, but it contains references to the same set of Employee objects that are in the original list. If you want the Employee objects in the two lists to be independent, then you have to copy them individually and put the copies into a new list.

查看更多
登录 后发表回答