继承CollectionBase来扩展自定义的集合类型

 Introduction
     很多时候我们在开发的过程中都要用到自定义集合类型.,CollectionBases此基类旨在使实施者创建强类型自定义集合变得更容易。实施者应扩展此基类,而不应创建自己的基类。所以我们在自定义集合类型的时候,不应该再创建自己的基类而应该扩展此基类.
 
Content
   下面代码就是通过继承CollectionBase来扩展自定义的Collection.

 using  System;
 nameSpace MyCollections
 {
      public class MyCollection: System.Collections.CollectionBase
     
     // Construct fuction
     public MyCollection()
      {
      // add code here
     }
   
 //Set the Index of this collection
 public object  this[int Index]
  {
     set
      {
          List[Index]   = value;
      }
     get
     {
         return  (oject)List[Index];
     }
 //Add Object into Collection
 public int Add(Object sender)
{
      return List.Add(sender);
}
// Remove Object
 pubic void Remove(Object sender)
{
   List.Remove(sender);
}
// Insert Object into assign positionn
public void Insert(int index,object sender)
{
   List.Insert(index,sender);
}
//IndexOf  assign object
public int IndexOf(object sender)
{
   reutrn List.IndexOf(sender);
}
//Judge object whether Collections is contain.
public bool Contains(object sender)
{
return List.Containers(sender);
}
 }
}
 因为CollectionBase类中包含了List的受属性,所以可以通过IList提供的接口来实现上述的功能.
有错误的地方请多加指点..3 Q...  
posted @ 2007-03-21 10:30  糊涂小猪  阅读(1759)  评论(0编辑  收藏  举报