DeepClone与ShadowClone(downmoon)
来源:互联网 发布:暗黑3网络加速器 编辑:程序博客网 时间:2024/06/07 07:03
杳MSDN知:
ArrayList.Clone 方法
创建ArrayList 的浅表副本。
命名空间:System.Collections
程序集:mscorlib(在 mscorlib.dll 中)
返回值ArrayList 的浅表副本。
集合的浅表副本仅复制集合的元素(不论它们是引用类型还是值类型),但不复制引用所引用的对象。新集合中的引用与原始集合中的引用指向相同的对象。
与之相对,集合的深层副本将复制这些元素以及由它们直接或间接引用的所有内容。
据此,说明如下 :
下面给出一个完整的例子:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace CloneObjectTest
{
// Just added to show the difference between shallow and deep cloning
[Serializable()]
class SampleRefType
{
public int Val;
}
[Serializable()]
class Employee
{
public string m_Name;
public Int16 m_age;
public SampleRefType someObject;
public void Print()
{
Console.WriteLine("Name : " + m_Name);
Console.WriteLine("Age : " + m_age);
Console.WriteLine("Some Value: " + someObject.Val);
}
// This is one way to do deep cloning. But works only if the
// objects and its references are serializable
public Employee DeepClone()
{
MemoryStream m = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(m, this);
m.Position = 0;
return (Employee)b.Deserialize(m);
}
// Do shallow cloning
public Employee ShallowClone()
{
return (Employee)this.MemberwiseClone();
}
static void Main(string[] args)
{
SampleRefType objRef = new SampleRefType();
objRef.Val = 1000;
Employee objA = new Employee();
objA.m_Name = "Manoj";
objA.m_age = 23;
objA.someObject = objRef;
Employee objB = objA.DeepClone();
Employee objC = objA.ShallowClone();
//objB is a deep clone. So chages made to objRef would not affect objB's value.
//But objC is shallow cloned. So, if the objRef is changed, so is the value in
//ObjC. So objC should print 2000 whereas objB should print 1000
objRef.Val = 2000;
//Updating some state
objC.m_age = 100;
objB.m_Name = "New Name";
Console.WriteLine("Original Object:");
objA.Print();
Console.WriteLine();
Console.WriteLine("Shallow Cloned Object:");
objC.Print();
Console.WriteLine();
Console.WriteLine("Deep Cloned Object:");
objB.Print();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace CloneObjectTest
{
// Just added to show the difference between shallow and deep cloning
[Serializable()]
class SampleRefType
{
public int Val;
}
[Serializable()]
class Employee
{
public string m_Name;
public Int16 m_age;
public SampleRefType someObject;
public void Print()
{
Console.WriteLine("Name : " + m_Name);
Console.WriteLine("Age : " + m_age);
Console.WriteLine("Some Value: " + someObject.Val);
}
// This is one way to do deep cloning. But works only if the
// objects and its references are serializable
public Employee DeepClone()
{
MemoryStream m = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(m, this);
m.Position = 0;
return (Employee)b.Deserialize(m);
}
// Do shallow cloning
public Employee ShallowClone()
{
return (Employee)this.MemberwiseClone();
}
static void Main(string[] args)
{
SampleRefType objRef = new SampleRefType();
objRef.Val = 1000;
Employee objA = new Employee();
objA.m_Name = "Manoj";
objA.m_age = 23;
objA.someObject = objRef;
Employee objB = objA.DeepClone();
Employee objC = objA.ShallowClone();
//objB is a deep clone. So chages made to objRef would not affect objB's value.
//But objC is shallow cloned. So, if the objRef is changed, so is the value in
//ObjC. So objC should print 2000 whereas objB should print 1000
objRef.Val = 2000;
//Updating some state
objC.m_age = 100;
objB.m_Name = "New Name";
Console.WriteLine("Original Object:");
objA.Print();
Console.WriteLine();
Console.WriteLine("Shallow Cloned Object:");
objC.Print();
Console.WriteLine();
Console.WriteLine("Deep Cloned Object:");
objB.Print();
}
}
}
- DeepClone与ShadowClone(downmoon)
- 关于时间随机数与Guid随机数(Downmoon)
- sql语句优化与时间成本(downmoon)
- Silverlight - 实现DeepClone方法
- asp.net与asp和平共处的奥秘--IIS(downmoon)
- SQL中通配符、转义符与"["号的使用(downmoon)
- 写程序的时间与头发的数量成反比(downmoon)
- SQL中通配符、转义符与"["号的使用(downmoon)
- SQL中Table型数据与用户自定义函数(downmoon)
- 对象克隆之DeepClone代码
- 冷藏 解冻 深拷贝deepclone
- 慎用JavaScript深复制!(js deepClone)
- DataGrid应用技巧两则(downmoon)---列求和与列字段转换d
- DataGrid应用技巧两则(downmoon)---列求和与列字段转换
- SQL中Table型数据(表变量)与用户自定义函数(downmoon)
- .net开发趣题两则-联想优秀程序员与专业程序员(downmoon).net
- DataGrid中的按钮反选事件与NamingContainer(命名容器)-downmoon
- DataGrid应用技巧两则(downmoon)---列求和与列字段转换
- CStatic控件的基本使用
- Windows下架设Subversion服务器
- DRB以及Rinda技术详解 (正在编写中...)
- dom4j修改节点属性及写入文件
- StringBuilder 和 String 的区别
- DeepClone与ShadowClone(downmoon)
- new有三种用法
- 不要过分设计
- C++ python STL 整数划分递归算法(即一个数分为几个数的和)
- 我的程序语言实践
- 我是不是也要总结一下我的2007
- C++ python STL 组合数递归算法(n个取m个数的组合数)
- 如何利用Grub命令启动损坏的Linux系统
- test