如何赋值 List < T > 而不将其作为对原 List < T > 的引用?

比如说

List<string> name_list1 = new List<string>();
List<string> name_list2 = new List<string>();

在代码的后面:

name_list1.Add("McDonald");
name_list1.Add("Harveys");
name_list1.Add("Wendys");


name_list2 = name_list1; // I make a copy of namelist1 to namelist2

因此,从这一点出发,我希望在不影响 name _ list1的情况下,继续在 name _ list2中添加元素或进行更改。我该怎么做?

121798 次浏览
name_list2 = new List<string>(name_list1);

This will clone the list.

Edit: This solution only works for primitive types. For objects, see other responses below.

name_list2 = new List<string>(name_list1); // Clone list into a different object

At this point, the two lists are different objects. You can add items to list2 without affecting list1

The problem is the assignment. Until the assignment name_list2 = name_list1;, you have two different List objects on the heap pointed to by the variables name_list1 and name_list2. You fill up name_list1, which is fine. But the assignment says, "make name_list2 point to the same object on the heap as name_list1." The List that name_list2 used to point to is no longer accessible and will be garbage collected. What you really want is to copy the contents of name_list1 into name_list2. You can do this with name_list10. Note that this will result in a "shallow" copy, which is fine for the example you cite, where the list contents are strings, but may not be what you want when the list members are more complex objects. It all depends on your needs.

Another Options is : Deep Cloning

public static T DeepCopy<T>(T item)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, item);
stream.Seek(0, SeekOrigin.Begin);
T result = (T)formatter.Deserialize(stream);
stream.Close();
return result;
}

so,

you can use :

name_list2 = DeepCopy<List<string>>(name_list1);

OR:

name_list2 = DeepCopy(name_list1);

will also work.

Here is an alternative solution:

    List<string> name_list1 = new List<string>();
List<string> name_list2 = new List<string>();


name_list1.Add("McDonald");
name_list1.Add("Harveys");
name_list1.Add("Wendys");


name_list2.AddRange(name_list1.ToArray());

The ToArray() method copies 'name_list1' to a new array, which we then add to name_list2 via the AddRange() method.

I like linq for this...

If list elements are primitives or structures then...

L2 = L1.ToList()

If list elements are classes then...

L2 = L1.Select(x => x.Copy()).ToList();

Where Copy could simply be a shallow copy exposure of MemberWiseClone, or it could be some implementation of a deep copy.

I prefer Json converter method to serialize and deserialize, this way you don't have to mark the classes for serialization, especially you have numerous child classes.

https://www.newtonsoft.com/json/help/html/SerializingJSON.htm

For primitive types:
List ClonedList = new list(OriginalList);

For non-primitive/User Defined types:
We need to perform a deep copy: Deep Copy is used to make a complete deep copy of the internal reference types, for this we need to configure the object returned by MemberwiseClone().

Step1- In your class inherit from ICloneable:
public class MyClass:ICloneable

Step2- Implement method

public MyClass Clone()
{
MyClass MyClassObj =new MyClass();
MyClassObj.Property1 = this.Property1;
.
.
MyClassObj.Property_N = this.Property_N;
return MyClass;
}

Step3- now clone your List

List<MyClass> MyClassClone = new List<MyClass>();
for(i=0; i<Count; i++)
{
MyClassClone.Add(OriginalClaaObj[i].Clone());
}

This will make deep copy of each item of the object.

None of the above solutions worked for me when using lists of class objects.

This can be used for copying any object to another object with shared property names.

public static void ObjectToObject(object source, object destination)
{
// Purpose : Use reflection to set property values of objects that share the same property names.
Type s = source.GetType();
Type d = destination.GetType();


const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;


var objSourceProperties = s.GetProperties(flags);
var objDestinationProperties = d.GetProperties(flags);


var propertyNames = objSourceProperties
.Select(c => c.Name)
.ToList();


foreach (var properties in objDestinationProperties.Where(properties => propertyNames.Contains(properties.Name)))
{


try
{
PropertyInfo piSource = source.GetType().GetProperty(properties.Name);


properties.SetValue(destination, piSource.GetValue(source, null), null);
}
catch (Exception ex)
{
throw;
}


}
}




public static List<T> CopyList<T>(this List<T> lst)
{
List<T> lstCopy = new List<T>();


foreach (var item in lst)
{
var instanceOfT = Activator.CreateInstance<T>();
ObjectToObject(item, instanceOfT);
lstCopy.Add(instanceOfT);
}
return lstCopy;
}

For lists use this: list2 = list1.CopyList();

For Primitive Types you can do this:

List<string> CopyList = new List<string>(OriginalList);

For non-primitve/user-difined types you can do this:

List<Person> CopyList = new List<Person>();
foreach(var item in OriginalList)
{
CopyList.Add(new Person {
Name = item.Name,
Address = item.Address
});
}

If both the lists are of the same complex type then you can do something like below:-

SomeClass List2 = new List();

List1.ForEach(u => List2.Add(u));

What I am doing is to loop through each element of List1 and keep adding it to List2. I believe this is the shortest way to do it.

While it could be potential performance-threat solution, but it would copy the values property-by-property eloquently.

using Newstonsoft.Json;


ClassA classA = new ClassA();
ClassA classACopyWithoutReference = JsonConvert.DeserializeObject<ClassA>(JsonConvert.SerializeObject(classA));

Based on @Mrunal answer I created an extension method:

public static T Clone<T>(this T source)
{
// Don't serialize a null object, simply return the default for that object
if (source == null)
{
return default;
}


// initialize inner objects individually
// for example in default constructor some list property initialized with some values,
// but in 'source' these items are cleaned -
// without ObjectCreationHandling.Replace default constructor values will be added to result
var deserializeSettings = new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace };


return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source), deserializeSettings);
}

And you can call it like this:

L2 = L1.Select(x => x.Clone()).ToList();

this is working for me using LINQ...

lst1= lst2.ToList();

this solution works For complex objects (Replace T with name of your Type):

list2 = list1.Concat(new List<T> { object }).ToList();

or:

list2 = list1.ToArray().Append(object).ToList()

You can clone the complex object by serialize and deserialize it, it will remove you object reference and create new object without reference

using Newstonsoft.Json;


List<string> name_list1 = new List<string>();
name_list1.Add("McDonald");
name_list1.Add("Harveys");
name_list1.Add("Wendys");


name_list2 = name_list1;


List<string> name_list2 = JsonConvert.DeserializeObject<List<string>>
(JsonConvert.SerializeObject(name_list1)); // Ii make a copy of namelist1 to namelist2