FCL(2) :: CollectionBase & Collection

本文探讨了在.NET框架中CollectionBase与泛型Collection<T>的不同之处,特别是在集合状态改变时通知外界的方式上的变化。作者对比了两种集合类的使用方式,并表达了对于新接口变更的观点。
CollectionBase是一个提供用户自定义Collection的一个基类.
在<<.net设计规范>> 第六章一条 避免在命名基类的时候使用"Base"后缀,如果该类会用于公用ApI.
例如:  CollectionBase Files(){}; 就是违反上述规范. 理由是增加"base"后缀只会对该方法的用户造成不必要的干扰.
现在在.net2.0 设计范型的集合基类命名为Collection<T>而非 CollecionBase<T>

另一个我们往往有需求就是在集合状态改变的时候通知外界,于是以前我们的做法就是继承CollectionBase,重写OnInsert(),OnInsertComplete()方法,触发事件通知外面
然而在Collection<T>范型集合类里却已找不到这样的接口,而是提供了InsertItem()这个的一个virtual方法.个人更偏好于CollectionBase这组接口,不知到2.0为何换了这样的接口.
ContractedBlock.gifExpandedBlockStart.gif难道要我们自己写成这样?
None.gifpublic class Persion
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
ExpandedBlockEnd.gif    }

None.gif    
public class PersonCollection : Collection<Persion>
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
protected override void InsertItem(int index, Persion item)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OnFireChanging();
InBlock.gif            
base.InsertItem(index, item);
InBlock.gif            OnFireChanged();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void OnFireChanged()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void OnFireChanging()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
public class ClildPerionCollecion : PersonCollection
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
protected override void InsertItem(int index, Persion item)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OnFireChanging();
InBlock.gif            
base.InsertItem(index, item);
InBlock.gif            OnFireChanged();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void OnFireChanging()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void OnFireChanged()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

Collection<T> 是放在 System.Collections.ObjectModel命名空间里,而非System.Collections.Generic命名空间中不知又是基于什么理由.

转载于:https://www.cnblogs.com/anders06/archive/2007/04/30/733191.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值