1024programmer Asp.Net c# list collection cloning

c# list collection cloning

c# list collection clone

In C#, the List collection is a generic collection that can store any type of object. Cloning a List collection can be achieved in the following ways:

  1. Using List’s constructor

Use the List constructor to create a new List object and copy the elements in the original List to the new List. For example:

List list1 = new List { 1, 2, 3 };
                 List list2 = new List(list1);
 

 

In the above code, list2 is a new List object that is initialized with the elements in list1.

  1. Use List’s CopyTo method

List’s CopyTo method can copy the elements in the original List to an array, and then copy the elements in the array to the new List. For example

 List list1 = new List { 1, 2, 3 };
                 List list2 = new List();
                 list1.CopyTo(list2.ToArray());
 

 

In the above code, list2 is a new List object that is initialized with the elements in list1.

  1. Use serialization

Another way to clone a List collection is through serialization and deserialization. You can serialize the original List object into a byte array, and then deserialize the byte array into a new List object. For example:

 List list1 = new List { 1, 2, 3 };
                 using (MemoryStream stream = new MemoryStream())
                 {
                     BinaryFormatter formatter = new BinaryFormatter();
                     formatter.Serialize(stream, list1);
                     stream.Position = 0;
                     List list2 = (List)formatter.Deserialize(stream);
                 }
 

In the above code, list2 is a new List object that is initialized using the elements in list1. This method can be used for any type of object, but it requires more computing resources and time.

3 methods of C# List reference type cloning

# Three methods for List reference type cloning, including reflection, serialization (depending on Newtonsoft.Json) and serialization (BinaryFormatter) implementation methods.

1. Reflection

/// 
         /// Clone via reflection
         /// 
         /// 
         /// 
         /// 
         public static List CloneReflect(this List list) where T : new()
         {
             List items = new List();
             foreach (var m in list)
             {
                 var model = new T();
                 var ps = model.GetType().GetProperties();
                 var properties = m.GetType().GetProperties();
                 foreach (var p in properties)
                 {
                     foreach (var pm in ps)
                     {
                         if (pm.Name == p.Name)
                         {
                             pm.SetValue(model, p.GetValue(m));
                         }
                     }
                 }
                 items.Add(model);
             }
             return items;
         }
 

 2. Serialization (depends on Newtonsoft.Json)

/// 
         /// Use third-party serialization method to clone - relies on Newtonsoft.Json
         /// 
         /// 
         /// 
         /// 
         public static List CloneNJson(this List list) where T : new()
         {
             var str = JsonConvert.SerializeObject(list);
             return JsonConvert.DeserializeObject<List>(str);
         }
 

3. Serialization (BinaryFormatter)

/// 
         /// Through the system's own serialized cloning
         /// 
         /// 
         /// 
         /// 
         public static List CloneBF(this List list)
         {
             using (Stream objectStream = new MemoryStream())
             {
                 IFormatter formatter = new BinaryFormatter();
                 formatter.Serialize(objectStream, list);
                 objectStream.Seek(0, SeekOrigin.Begin);
                 return (List)formatter.Deserialize(objectStream);
             }
         }
 

 

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/c-list-collection-cloning/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索