c#小技巧3

本文通过C#实现展示了深拷贝与浅拷贝的区别。深拷贝完全复制对象及其引用的对象,而浅拷贝仅复制对象本身,对于引用类型的字段只复制引用。示例代码使用了`BinaryFormatter`来序列化对象并实现深拷贝。
1. 深拷贝与浅拷贝
深拷贝,对对象整体进行拷贝包括对象的值数据和引用到的对象。
浅拷贝,只拷贝对象的非静态字段,如值类型数据。如果字段为引用字段那么只拷贝引用,因此引用到的对象还是原来的对象。
None.gif    // 这个标志是必须的,告诉assembly该类可以序列化
None.gif
    [Serializable]
None.gif    
class Aphla
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private string _name;
InBlock.gif        
private Beta _beta;
InBlock.gif
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _name = value; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _name; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string BetaName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _beta.Name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Aphla(Beta b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _beta 
= b;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Display(string objName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"{0}, {1}", objName, _name);
InBlock.gif            _beta.Display();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// 深拷贝对象
InBlock.gif
        public Aphla Clone()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Aphla a;
InBlock.gif            
// 创建流用于保存要被序列化的对象
InBlock.gif
            MemoryStream ms = new MemoryStream();
InBlock.gif            
// 创建二进制格式器,用于对序列化对象的流进行格式化
InBlock.gif
            BinaryFormatter bf = new BinaryFormatter();
InBlock.gif
InBlock.gif            
// 将指定的对象以创建的格式器序列到流中
InBlock.gif
            bf.Serialize(ms, this);
InBlock.gif            
// 将流的游标置回开始点
InBlock.gif
            ms.Position = 0;
InBlock.gif
InBlock.gif            
// 反序列化流到对象
InBlock.gif
            a = (Aphla)bf.Deserialize(ms);
InBlock.gif            
return a;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// 浅拷贝对象
InBlock.gif
        public Aphla ShallowClone()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Aphla a 
= (Aphla)this.MemberwiseClone();
InBlock.gif            
return a;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif
None.gif    
public class Beta
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private string _name;
InBlock.gif
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Display()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"Beta: {0}", _name);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值