当前位置:网站首页>c#浅拷贝与深拷贝自定义的类的List<>
c#浅拷贝与深拷贝自定义的类的List<>
2022-06-10 13:56:00 【GreenHandBruce】
浅拷贝只需要直接赋值,
深拷贝需要写一个函数,通过序列化与反序列化将旧的list<>对象拷贝到新的List<>对象,具体代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LstCopy
{
public partial class Form1 : Form
{
[Serializable]
public class USER
{
public string name;
public int id;
}
public Form1()
{
InitializeComponent();
List<USER> users = new List<USER>();
for(int i=0;i< 10;i++)
{
USER user = new USER();
user.name = "name" + i.ToString();
user.id = i;
users.Add(user);
}
List<USER> tempLst = Clone<USER>(users);//深拷贝
//List<USER> tempLst = users;//浅拷贝
tempLst = tempLst.FindAll(p=>p.id>5).ToList();
tempLst[0].name = "changed";
}
public static List<T> Clone<T>(object List)
{
using (Stream objectStream = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(objectStream, List);
objectStream.Seek(0, SeekOrigin.Begin);
return formatter.Deserialize(objectStream) as List<T>;
}
}
}
}
边栏推荐
猜你喜欢
![[technical analysis] discuss the production process and technology of big world games - preliminary process](/img/8d/52cb98a617f690df310d6de5512ebc.png)
[technical analysis] discuss the production process and technology of big world games - preliminary process

What does the multi cloud management platform CMP mean? Who can explain it clearly

【FAQ】運動健康服務REST API接口使用過程中常見問題和解决方法總結
![buuctf [Discuz]wooyun-2010-080723](/img/b9/d63c9c638ff7c390c490d6fd5eafd1.png)
buuctf [Discuz]wooyun-2010-080723

解决跨海高并发崩溃难题?so easy

解决VMware Workstation安装VMware Tools显示灰色的办法

22.6.7成功使用doc2vec模型生成嵌入向量

【专题介绍】圆桌论坛——AI与音视频技术的融合之路

The shortcomings of the "big model" and the strengths of the "knowledge map"

大厂面试上午10:00面试,10:09就出来了 ,问的实在是太...
随机推荐
MMdetection增加评估指标precision
Gorm设置外键
Google Earth engine (GEE) - real time global 10 meter land use / land cover (LULC) data set based on S2 images
工作中记录MySQL中的常用函数
Flutter drawer学习总结6
Net core Tianma XingKong series - Interface Implementation for dependency injection and mutual conversion of database tables and C entity classes
[Huang ah code] I cleaned the console of Google browser in this way
组装芯片难保竞争优势,痛定思痛的高通终于开始自研核心架构
Win10 virtual machine download and installation process
NC|王军/宋默识结合三代测序解析肠道菌群结构变异和功能
Leetcode 829. Sum of continuous integers
解决跨海高并发崩溃难题?so easy
What happened when the legendary login prompt failed to connect to the server? How to solve it?
Review summary of final examination of software architecture principles, methods and practices, Second Edition
Qualcomm has finally begun to develop its own core architecture after learning from the difficulties of assembling chips to maintain its competitive advantage
Google Earth engine (GEE) -- batch download of DEM using MODIS leaf area index image mask
Leetcode-56-merge interval
im即时通讯开发:进程被杀底层原理、APP应对被杀技巧
Markdown sets the font to red
Textinputlayout usage details