C# CollectionBase的应用
2008-03-31 14:49
232 查看
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的数据一一配对的数据的存储。
这样一看,我猜大家对这两个集合就不会有陌生感了。用起来也会相当顺手。
相关文章推荐
- C#中的CollectionBase用法
- C#.NET中的base关键字在派生类里的应用
- c# base和this在构造函数中的应用
- c# base和this在构造函数中的应用
- C#_Hastable collection中哈希表应用
- c#学习-base和this在构造函数中的应用
- C# CollectionBase,ICloneeable
- Sqlite在C#中的应用(转)
- C#使用Xamarin开发可移植移动应用进阶篇(9.混淆代码,防止反编译)
- C#应用MemoryStream提高File读取速度
- (转)C#开发微信门户及应用(4)--关注用户列表及详细信息管理
- 在C#中应用哈希表(Hashtable)
- C#之base和this关键字内部神秘面纱
- C#开发微信门户及应用(31)--微信语义理解接口的实现和处理
- C# 在EXE应用中集成dll(调用内部dll链接库)
- C# 反射技术应用
- 介绍一下C#中delegate的应用
- C#中FlagsAttribute属性在enum中的应用
- C# 集合(Collection)
- Andbase应用开发框架