Home  >  Article  >  Backend Development  >  Briefly introduce the deep copy problem of List object in C#

Briefly introduce the deep copy problem of List object in C#

黄舟
黄舟Original
2017-03-29 11:32:421683browse

The editor below will bring you a brief discussion on the deep copy problem of List8742468051c85b06f0a0af9e3e506b5cobject in C#. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

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 methods. Only the references to objects in the List will be copied. You can use the following extension methods 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 way 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. Test the deep copy of the above objects

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 is the detailed content of Briefly introduce the deep copy problem of List object in C#. For more information, please follow other related articles on 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