C# CollectionBase的应用

using  System.Collections;
using  System;

namespace  abcd
{
    
public class DocumentCollection : CollectionBase
    
{
        
public DocumentCollection() { }
        
public DocumentCollection(DocumentCollection value)
        
{
            
this.AddRange(value);
        }

        
public DocumentCollection(string[] value)
        
{
            
this.AddRange(value);
        }


        
public string this[int index]
        
{
            
get return ((string)(List[index])); }
            
set { List[index] = value; }
        }


        
public int Add(string value)
        
{
            
return List.Add(value);
        }

        
public void AddRange(string[] value)
        
{
            
for (int Counter = 0; Counter < value.Length; Counter = Counter + 1)
            
{
                
this.Add(value[Counter]);
            }

        }

        
public void AddRange(DocumentCollection value)
        
{
            
for (int Counter = 0; Counter < value.Count; Counter = Counter + 1)
            
{
                
this.Add(value[Counter]);
            }

        }

        
public void Remove(string value)
        
{
            List.Remove(value);
        }

        
public void Insert(int index, string value)
        
{
            List.Insert(index, value);
        }

        
public bool Contains(string value)
        
{
            
return List.Contains(value);
        }

        
public int IndexOf(string value)
        
{
            
for (int i = 0; i < List.Count; i++)
            
{
                
if ((string)List[i] == value)
                
{
                    
return i;
                }

            }

            
return -1;
        }

        
public string FindById(int documentId)
        
{
            
foreach (string document in List)
            
{
                
if (document==(string)List[documentId])
                
{
                    
return document;
                }

            }

            
return null;
        }

        
public void CopyTo(string[] array, int index)
        
{
            List.CopyTo(array, index);
        }


        
public string this[int index]
        
{
            
get
            
{
                
return (string)List[index];
            }

            
set 
            
{
                List[index] 
= value;
            }

        
        }


        
public new DocumentEnumerator GetEnumerator()
        
{
            
return new DocumentEnumerator(this);   //这里是需要注意的地方,返回的是一个类但是必须该类实现
IEnumerator接口,所以下面定义的内部类就继承了 IEnumerator接口
        }



        
static void Main(string[] args)
        
{
            DocumentCollection aa 
= new DocumentCollection();
            aa.Add(
"aaaa");
            aa.Add(
"bbbb");
            aa.Add(
"cccc");
            
foreach (string i in aa)
            
{
                Console.Write(i);
            }


            Console.Read();
        }


    }


    
public class DocumentEnumerator : IEnumerator
    
{
        
private IEnumerator Base;   
        
private IEnumerable Local;   

        
public object Current
        
{
            
get return Base.Current; }
        }


        
public DocumentEnumerator(DocumentCollection Documents)
        
{
            Local 
= Documents;   //将Documents转换为接口IEnumerable ,然后调用的CollectionBase方法才是CollectionBase的
                                                 CollectionBase方法,如果不转换的话,仍然是我们重写的GetEnumerator方法,所以会出现死循环,                                                
                                               这里需要注意
            Base 
= Local.GetEnumerator();
        }


        
public bool MoveNext()
        
{
            
return Base.MoveNext();
        }


        
public void Reset()
        
{
            Base.Reset();
        }

    }



}


在网上对 CollectionBase只有大体的介绍,似乎不是太详细,今天整理出此实例对 CollectionBase有较为完整的理解,也是一个非常典型的集合,特整理出来,以供日后方便

写到这想起了c#的集合,现不包括泛型的话,通常我们会使用arraylist 和hashtable来进行存储。
Arraylist索引是根据序号,我们看微软的源代码就可知它的容器是通过一维数组来存储的,以及所有针对它的add,remove等操作都是通过对该数组来进行操作显示的。
而hashtable即哈希表的内部容器也是一个数组数组包含的是实例化后的structure类,该类具有key和value的属性,这样一来即可帮助我们完成具有key和value的数据一一配对的数据的存储。
这样一看,我猜大家对这两个集合就不会有陌生感了。用起来也会相当顺手。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值