当前位置:
首页 > temp > 简明python教程 >
-
C# ICloneable,shallow clone,deep clone.
1 [Serializable] 2 public class Person:ICloneable 3 { 4 public string Name { get; set; } 5 public int Id { get; set; } 6 public int Age { get; set; } 7 public Department Department { get; set; } 8 9 public object Clone() 10 { 11 return this.MemberwiseClone(); 12 } 13 14 15 public Person ShallowClone() 16 { 17 return Clone() as Person; 18 } 19 20 public Person DeepClone() 21 { 22 using (Stream objStream = new MemoryStream()) 23 { 24 IFormatter formatter = new BinaryFormatter(); 25 formatter.Serialize(objStream, this); 26 objStream.Seek(0, SeekOrigin.Begin); 27 return formatter.Deserialize(objStream) as Person; 28 } 29 } 30 } 31 32 [Serializable] 33 public class Department 34 { 35 public string DeptName { get; set; } 36 public int DeptId { get; set; } 37 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 CloneDemo(); 6 Console.ReadLine(); 7 } 8 9 static void CloneDemo() 10 { 11 Person fred = new Person() 12 { 13 Id = 1, 14 Name = "Fred", 15 Age = 32, 16 Department=new Department() 17 { 18 DeptId=1, 19 DeptName="Dept1" 20 } 21 }; 22 23 Console.WriteLine($"fred,name:{fred.Name},Id:{fred.Name},department name:{fred.Department.DeptName},deptid:{fred.Department.DeptId}"); 24 25 Person shallowClonePerson = fred.ShallowClone() as Person; 26 Console.WriteLine($"shallowClonePerson,Name:{shallowClonePerson.Name},Id:{shallowClonePerson.Id},department name:{shallowClonePerson.Department.DeptName},deptId:{shallowClonePerson.Department.DeptId}"); 27 28 fred.Id = 2; 29 fred.Name = "fred2"; 30 fred.Age = 33; 31 fred.Department.DeptId = 2; 32 fred.Department.DeptName = "dept2"; 33 Console.WriteLine($"shallowClonePerson,Name:{shallowClonePerson.Name},Id:{shallowClonePerson.Id},department name:{shallowClonePerson.Department.DeptName},deptId:{shallowClonePerson.Department.DeptId}"); 34 35 36 Person deepClonePerson = fred.DeepClone() as Person; 37 Console.WriteLine($"deepClonePerson,Name:{deepClonePerson.Name},Id:{deepClonePerson.Id},department name:{deepClonePerson.Department.DeptName},id:{deepClonePerson.Department.DeptId}"); 38 39 fred.Id = 3; 40 fred.Name = "Fred3"; 41 fred.Age = 34; 42 fred.Department.DeptId = 3; 43 fred.Department.DeptName = "dept3"; 44 Console.WriteLine($"deepClonePerson,Name:{deepClonePerson.Name},Id:{deepClonePerson.Id},department name:{deepClonePerson.Department.DeptName},id:{deepClonePerson.Department.DeptId}"); 45 } 46 }
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数