C# - 集合类 - 集合接口

时间:2023-03-09 05:02:33
C# - 集合类 - 集合接口

本篇将介绍关于集合的接口 这些接口定义了所有与集合有关的类的框架

IEnumerable接口

ns:System.Collections

此接口定义了对集合遍历的方法 一般表示元素序列或集合的类都实现了此接口 包括String类

IEnumerable接口的方法

GetEnumerator方法

获取一个用于遍历集合的枚举器 在许多遍历的应用中GetEnumerator方法被后台调用以实现foreach循环

IEnumerator接口

ns:System.Collections

此接口定义了一些属性与方法来遍历集合 表示一个集合枚举器

IEnumerator接口的属性

Current属性

获取当前正被枚举器所访问的元素

IEnumerator接口的方法

MoveNext方法

使枚举器指向集合中的下一个元素 并返回一个bool值 true表示顺利指向了下一个元素 false表示已达到集合末端

Reset方法

把枚举器重置到集合的最开始位置

 string str = "hello";
 IEnumerator tor = str.GetEnumerator();
 while (tor.MoveNext())
 {
     Console.WriteLine(tor.Current);
 }

最后注意 MoveNext方法只能遍历未做修改的集合 如果遍历集合后 在后来又修改了集合 再次遍历集合 则此方法将抛出异常 提示集合已修改 无法遍历 所以通常的做法是使用foreach循环

IDictionaryEnumerator接口

ns:System.Collections

此接口提供用于访问字典集合数据的方法 表示一个字典集合枚举器 此接口继承于IEnumerator接口

IDictionaryEnumerator接口的属性

Entry

获取一个表示集合中的某一个项的对象 该对象是一个DictionaryEntry类型

Key

获取集合中当前项的键

Value

获取集合中当前项的值

 Hashtable table = new Hashtable();
 table.Add(", "sam");
 table.Add(", "leo");
 IDictionaryEnumerator tor= table.GetEnumerator();
 while (tor.MoveNext())
 {
     Console.WriteLine("key:{0}   value:{1}",tor.Entry.Key,tor.Entry.Value);
 }

如果使用了MoveNext遍历集合后又修改了集合 再次遍历集合 则会抛出异常 最好改为foreach

 Hashtable table = new Hashtable();
 table.Add(", "sam");
 table.Add(", "leo");

 foreach (DictionaryEntry entry in table)
 {
     Console.WriteLine("key:{0}   value:{1}", entry.Key, entry.Value);
 }

 table.Add(","korn");

 foreach (DictionaryEntry entry in table)
 {
     Console.WriteLine("key:{0}   value:{1}", entry.Key, entry.Value);
 }

ICollection接口

ns:System.Collections

此接口提供了获取集合元素总数和复制集合元素到数组的方法 继承层次为:IEnumerable > ICollection

ICollection接口的属性

Count属性

获取集合元素总数

ICollection接口的方法

CopyTo方法

将集合中的元素拷贝到参数指定的数组中 可以通过参数2指定数组下标 表示集合中的元素要拷贝到数组的起始位置

IList接口

ns:System.Collections

此接口提供了以索引的方式访问集合元素的索引器 继承层次为:IEnumerable > ICollection > IList

IList接口的属性

this[index]索引器

通过集合元素的索引来取得元素

IList接口的方法

Add方法

将对象添加到集合

Clear方法

清除集合中所有项

Contains方法

集合中是否存在参数指定的元素

IndexOf方法

在集合中查找参数指定的元素 找到后返回元素的下标 找不到返回-1

Insert方法

将参数指定的元素插入到参数指定的下标处 如果指定的下标处已经存在x元素 则x元素及其后边的元素将向后移动

Remove方法

移除集合中由参数指定的元素 无论集合中是否存在多个相同的元素 只移除最先与参数匹配的元素

RemoveAt方法

移除集合中由参数指定的下标处的元素

IDictionary接口

ns:System.Collections

此接口提供了以键值对的方式访问集合元素的方法 继承层次为:IEnumerable > ICollection > IDictionary

IDictionary接口的属性

this[key]索引器

通过集合元素的key来取得元素的值

Keys属性

获取集合中所有元素的Key

Values属性

获取集合中所有元素的值

IDictionary接口的方法

Add方法

向集合中添加带有键值的项

Clear方法

清除集合中所有项

Contains方法

集合中是否存在参数指定的key

Remove方法

从集合中移除参数指定的项 如果元素从集合中移除 它的位置会被其他元素占位

IComparer接口

ns:System.Collections

此接口只定义了一个用于比较两个对象的Compare方法 它与IComparable接口的区别在于 IComparable提供的CompareTo方法只接收一个对象与调用该方法的对象做比较 而IComparer提供的Compare方法接收两个对象用于比较 此接口由Comparer、CaseInsensitiveComparer、KeysConverter(System.Windows.Forms)实现

集合接口的实现类

Stack类和Queue类实现了ICollection、IEnumerable、ICloneable接口

ArrayList类实现了IList、ICloneable接口

Hashtable类和SortedList类实现了IDictionary、ICloneable接口

另外还有CollectionBase类和DictionaryBase类 这两个类可以看成是两个关于集合的基类 但上面提到的集合类并非派生自CollectionBase或DictionaryBase 而是直接派生自Object 它们通过实现上面提到的接口来提供有关集合的操作 CollectionBase类和DictionaryBase类主要用于实现自定义的集合类 当内置的集合类不满足你的需求时 可以考虑派生CollectionBase类或者DictionaryBase类  下面的示例使用CollectionBase实现了自定义集合 并在自定义集合的Add方法中输出正被添加的元素

 public class MyList : CollectionBase
 {
     public void Add(string name)
     {
         base.List.Add(name);
         Console.WriteLine("添加了新项{0}", name);
     }

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

 public static void Main(string[] args)
 {
     MyList list = new MyList();
     list.Add("sam");
     list.Add("leo");
 }

可以看到在Add方法内部是调用基类CollectionBase的List来添加项的 同样的也可以实现自定义的字典集合类 需要将其派生自DictionaryBase

 public class MyDictionary : DictionaryBase
 {
     public void Add(object Key,object Value)
     {
         base.Dictionary.Add(Key, Value);
         Console.WriteLine("添加了新项{0}", Value);
     }

     public string this[object Key]
     {
         get { return base.Dictionary[Key].ToString(); }
         set { base.Dictionary[Key] = value; }
     }
 }

 public static void Main(string[] args)
 {
     MyDictionary list = new MyDictionary();
     list.Add(,"sam");
     list.Add(,"leo");
 }

如果需要自定义泛型集合 则可以考虑实现IEnumerable<T> ……

C# - 学习总目录