当前位置:网站首页>C daily development notes -- solve the problem that one set references another set, resulting in changes together
C daily development notes -- solve the problem that one set references another set, resulting in changes together
2022-04-22 07:12:00 【Caramel macchiato】
Deep copy ( Encapsulated as a generic generic generic method, copy and use ):
/// <summary>
/// Deep copy set method ---- Solve the problem that one set refers to another set, resulting in changes together
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list1"></param>
/// <param name="list2"></param>
/// <returns></returns>
public static List<T> DeepCopyList<T>(IList<T> list1 , out List<T> list2)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, list1);
ms.Position = 0;
list2 = (List<T>)bf.Deserialize(ms);
return list2;
}
Class used ( Class needs to be marked with serializable attributes ):
[Serializable]
public class Student
{
public int Id {
get; set; }
public int Age {
get; set; }
}
The test method :
static void Main(string[] args)
{
var list1 = new List<Student>();
Student stu1 = new Student();
stu1.Id = 1;
stu1.Age = 12;
list1.Add(stu1);
Student stu2 = new Student();
stu2.Id = 2;
stu2.Age = 21;
list1.Add(stu2);
Student stu3 = new Student();
stu3.Id = 3;
stu3.Age = 23;
list1.Add(stu3);
var list2 = new List<Student>();
list2 = DeepCopyList<Student>(list1,out list2).Where(s => s.Id == 1).ToList();
var ss = list2.Select(s => s.Age = 33).ToList();
}
result :

turn json
/// <summary>
/// turn json Method ---- Solve the problem that one set refers to another set, resulting in changes together
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static List<T> Clone<T>(this List<T> list) where T : new()
{
var str = JsonConvert.SerializeObject(list);
return JsonConvert.DeserializeObject<List<T>>(str);
}
Class used ( Class needs to be marked with serializable attributes ):
[Serializable]
public class Student
{
public int Id {
get; set; }
public int Age {
get; set; }
}
The test method :
static void Main(string[] args)
{
var list1 = new List<Student>();
Student stu1 = new Student();
stu1.Id = 1;
stu1.Age = 12;
list1.Add(stu1);
Student stu2 = new Student();
stu2.Id = 2;
stu2.Age = 21;
list1.Add(stu2);
Student stu3 = new Student();
stu3.Id = 3;
stu3.Age = 23;
list1.Add(stu3);
var list2 = new List<Student>();
list2 = Clone<Student>(list1).Where(s => s.Id == 1).ToList();
var ss = list2.Select(s => s.Age = 33).ToList();
}
result :

版权声明
本文为[Caramel macchiato]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220608525231.html
边栏推荐
猜你喜欢

微电子专业是做芯片的吗?芯片和什么专业有关?

集成电路模拟版图入门-版图基础学习笔记(二)

转IC设计需要补充哪方面的知识技能?

Typec to HDMI + PD3 0 + U3 + U2 + SD / TF card reading expansion seven in one scheme design circuit | cs5266 + ma8621 design reference circuit

Alternative ag9311 design circuit | cs5266 application circuit diagram | design and development of three in one scheme of typec docking station

MySQL完全卸载,mysql服务清理

分布式任务调度与计算框架:PowerJob 快速开始(本地IDE版) 02

Nacos持久化切换配置

集成电路模拟版图入门-版图基础学习笔记(三)

STM32学习记录0001——学习准备
随机推荐
Statistical categories in 911 from 2015 to 2017
SSS1700
再见了Postman,有一说一:Apifox才是YYDS
分页
Nacos之服务配置中心
leetcode1218:最长定差子序列
集成电路模拟版图入门-版图基础学习笔记(五)
Changes in the number of different types of calls in different months in 911 data
Scheme selection table of USB audio chip / typec audio and video processing chip
Application of ring network redundant can optical transceiver in Baldwin fire alarm system
MySQL完全卸载,mysql服务清理
MATLAB:去除音频信号噪音
STM32 learning record 0001 - learning preparation
STM32学习记录0003——STM32芯片解读
STM32 learning record 0005 - JLINK download and debugging
Pl2586 design circuit | pl2586 replaces Fe1 1s circuit diagram | USB2 Scheme design of 0hub industrial hub
转IC设计需要补充哪方面的知识技能?
Application of can optical transceiver in fire networking
集成电路模拟版图入门-版图基础学习笔记(四)
Nacos集群配置