Why does copy of the List still change properties in the original List using C#

后端 未结 6 1710
不知归路
不知归路 2021-01-21 02:49

Lets say I have this class

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


        
6条回答
  •  Happy的楠姐
    2021-01-21 03:34

    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.

提交回复
热议问题