Home  >  Article  >  Backend Development  >  A brief discussion on the deep copy problem of List objects in C#

A brief discussion on the deep copy problem of List objects in C#

高洛峰
高洛峰Original
2017-01-19 13:09:481461browse

1. When T in the List8742468051c85b06f0a0af9e3e506b5c object is a value type (int type, etc.)

For a List of value types, you can copy it directly using the following method:

List<T> oldList = new List<T>();
oldList.Add(..);
List<T> newList = new List<T>(oldList);

2. When T in the List8742468051c85b06f0a0af9e3e506b5c object is a reference type (such as a custom entity class)

1. Lists of reference types cannot be copied using the above method, only To copy the reference of the object in the List, you can use the following extension method to copy:

static class Extensions
 {
     public static IList<T> Clone<T>(this IList<T> listToClone) where T: ICloneable
     {
         return listToClone.Select(item => (T)item.Clone()).ToList();
     }
 //当然前题是List中的对象要实现ICloneable接口
 }

2. Another method is to use serialization to complete a deep copy of the reference object. This method is the most reliable

public static T Clone<T>(T RealObject)
 
{
   using (Stream objectStream = new MemoryStream())
   {
      //利用 System.Runtime.Serialization序列化与反序列化完成引用对象的复制
       IFormatter formatter = new BinaryFormatter();
       formatter.Serialize(objectStream, RealObject);
       objectStream.Seek(0, SeekOrigin.Begin);
       return (T)formatter.Deserialize(objectStream);
   }
}

3. Use System.Xml.Serialization to implement serialization and deserialization

public static T Clone<T>(T RealObject)
{
      using(Stream stream=new MemoryStream())
      {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        serializer.Serialize(stream, RealObject);
        stream.Seek(0, SeekOrigin.Begin);
        return (T)serializer.Deserialize(stream);
      }
}

3. Deep copy the above objects Carry out the test

The test is as follows:

using System;
using System.Collections.Generic;
using System.Collections ;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
 
namespace LINQ
{
  [Serializable]
  public class tt
  {
    private string name = "";
 
    public string Name
    {
      get { return name; }
      set { name = value; }
    }
    private string sex = "";
 
    public string Sex
    {
      get { return sex; }
      set { sex = value; }
    }
  }
 
  class LINQTest
  {
    public static T Clone<T>(T RealObject)
    {
      using (Stream objectStream = new MemoryStream())
      {
        IFormatter formatter = new BinaryFormatter();
        formatter.Serialize(objectStream, RealObject);
        objectStream.Seek(0, SeekOrigin.Begin);
        return (T)formatter.Deserialize(objectStream);
      }
    }
 
 
    public static void Main()
    {
      List<tt> lsttt = new List<tt>();
      tt tt1 = new tt();
      tt1.Name = "a1";
      tt1.Sex = "20";
      lsttt.Add(tt1);
      List<tt> l333 = new List<tt>();
      l333.Add(Clone<tt>(lsttt[0]));
      l333[0].Name = "333333333";
   }
 }
}

The above article briefly discusses the deep copy problem of List objects in C# is all the content shared by the editor. I hope it can To give you a reference, I also hope that everyone will support the PHP Chinese website.

For more articles on the deep copy problem of List8742468051c85b06f0a0af9e3e506b5c objects in C#, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn