zoukankan      html  css  js  c++  java
  • C# 集合类-接口

    所谓,程序=数据结构+算法。

    我目前的日常工作就是繁琐的业务流程和增删改查之类的。

    其实繁琐的业务流程也不过是改变一下数据的状态。怪不得叫,面向数据库编程。哈哈。

    所以呢,了解一下各种 .net内置的集合类对我来说是有很大帮助的。

    这篇大概讲一下集合类的接口。


    在 System.Collections, 命名空间下的是关于集合的接口以及系统集合类的一些定义

     1 public interface ICollection:IEnumerable
     2 public interface IComparer
     3 public interface IDictionary:ICollection,IEnumerable
     4 public interface IDictionaryEnumerator:IEnumerato
     5 public interface IEnumerable
     6 public interface IEnumerator
     7 public interface IEqualityComparer
     8 public interface IHashCodeProvider
     9 public interface IList:ICollection,IEnumerable
    10 internal class KeyValuePairs



    1 //枚举接口
    2 public interface IEnumerator
    3 {
    4   bool MoveNext();
    5   object Current{get;}
    6   void Reset();
    7 }

    这个方法接口里有两个方法,分别是移动到下一个项和复位初使。
    还有一个属性,这个属性的返回值是object。
    这个移动是单向的,只能一个方向移动,不可以自己选移动的方向和条目。

    这个可以认为是集合类必须实现的基本接口。


    //返回枚举接口
    public interface IEnumerable
    {
        IEnumerator GetEnumerator();
    }

    在这两个接口的基础上形成其他的接口。

     1 //有序集合
     2 public interface ICollection : IEnumerable
     3 {
     4   //把集合对象拷贝
     5   void CopyTo(Arrayarray,intindex);
     6   //集合的元素数目
     7   intCount{get;}
     8   //返回集合是否是多线程同步访问
     9   boolIsSynchronized{get;}
    10   //返回同步对象
    11   objectSyncRoot{get;}
    12 }
     1 //索引集合
     2 public interface IList : ICollection,IEnumerable
     3 {
     4   int Add(object value);
     5   void Clear();
     6   bool Contains(object value);
     7   int IndexOf(object value);
     8   void Insert(int index,object value);
     9   void Remove(object value);
    10   void RemoveAt(int index);
    11 
    12   bool IsFixedSize{get;}
    13   bool IsReadOnly{get;}
    14   object this[intindex]{get;set;}
    15 }

    这个接口就很丰富了,有增加成员,删除成员等一些方法。

     1 //字典集合或键值集合
     2 public interface IDictionary : ICollection,IEnumerable
     3 {
     4   void Add(objectkey,objectvalue);
     5   void Clear();
     6   bool Contains(object key);
     7   IDictionaryEnumerator GetEnumerator();
     8   void Remove(object key);
     9  
    10   bool IsFixedSize{get;}
    11   bool IsReadOnly{get;}
    12   object this[object key]{get;set;}
    13   ICollection Keys{get;}
    14   ICollection Values{get;}
    15 }

    集合类型都实现了 IEnumerable(返回枚举接口) 接口,从而可以使用 foreach 迭代循环。
    实现了 ICollection(有序集合) 接口的集合类表明集合中的元素是有先后顺序的。
    IList 接口继承了 ICollection(有序集合) 接口,实现了 IList 接口的集合类不止表明集合中的元素是有先后顺序,而且表明集合类可以通过下标访问的方式来访问集合元素。
    IDictionary 接口也继承了 ICollection(有序集合) 接口,实现了 IDicionary 接口的集合类可以通过下标 key 访问的访问方式访问集合元素。

    ICollection 接口扩展了 IEnumerable;
    IDictionary 和 IList 则是扩展 ICollection 的更为专用的接口。
    IDictionary 实现是键/值对的集合,如Hashtable类。
    IList 实现是值的集合,其成员可通过索引访问,如 ArrayList 类。

    某些集合(如 Queue 类和 Stack 类)限制对其元素的访问,它们直接实现ICollection接口。
    如果 IDictionary 接口和 IList 接口都不能满足所需集合的要求,则从 ICollection 接口派生新集合类以提高灵活性。

    在此 ILIST 接口的基础上产生的一些类型 是我们实际编程中能用的到的,比如

    1 //数组类这个类是抽象的
    2 public abstract class Array : ICloneable, IList, ICollection, IEnumerable

    我们平时用到的比如 int[] intarray = new int[] {} 是派生于这个抽象类。

    但是数组是从 Array 隐式派生的,这是由编译器完成的,所以我们看不到具体的实现过程。

    比如

    1 Array array1;
    2 int[]a=newint[]{};
    3 string[]b=newstring[]{};
    4 array1=a;
    5 array1=b

    因为数组的大小是固定的,类型也是固定的。就发明了 ArrayList,它是一个长度可变的object数组。

    1 public class ArrayList : IList, ICollection, IEnumerable, ICloneable

    object 可以传入任何类型但是涉及装箱和拆箱的操作,所以性能受影响。
    Stack和Queue栈和队列是有序集合。
    HashTable 实现了 IDictionary 接口。是键值对的集合。
    也是键和值都是object但是索引,集合下标是int,value是object
    但是hashtable键值不是按照插入的顺序进行排列。

    1 public class SortedList : IDictionary, ICollection, IEnumerable, ICloneable

    SortList 也是字典类,但是按照顺序排放.
    非范性的集合类就这些了。


    泛型集合类


    System.Collections.Generic 是.NET2.0新增的一个命名空间。

    C#1.0 中的集合类型都存在着装箱,拆箱的性能问题以及潜在的类型安全问题。

    丑陋的设计必须得到改进,于是 C#2.0 就引入了泛型这个概念。

    泛型即解决了装箱拆箱的性能问题,又解决了潜在的类型转换安全问题,所以在实际应用中,推荐使用泛型集合代替非泛型集合。(泛型实际是编译器帮我们生成了代码)

    1 //这个泛型接口继承自非泛型的同名接口
    2 public interface IEnumerator<T> : IDisposable , IEnumerator
    3 {
    4     bool MoveNext();
    5     T Current {  get; }
    6     void Reset();
    7 }
    1 //返回枚举 泛型的同名接口
    2 public interface IEnumerable<T> : IEnumerable//
    3 {
    4   IEnumerator<T>  GetEnumerator();
    5 }

    IEnumerable 只有一个方法 GetEnumerator() 即得到遍历器。

     1 //有序集合
     2 public interface ICollection<T> : IEnumerable<T>,IEnumerable
     3 {
     4   void Add(Titem);
     5   void Clear();
     6   bool Contains(Titem);
     7   void CopyTo(T[]array,intarrayIndex);
     8   bool Remove(Titem);
     9 
    10   int Count{get;}
    11   bool IsReadOnly{get;}
    12 }

    ICollection 与 ICollection<T> 略有不同,

    ICollection 不提供编辑集合的功能,即 Add() 和 Remove()。包括检查元素是否存在的 Contains 也不支持。

     1 public interface IDictionary<TKey,TValue> : ICollection<KeyValuePair<TKey,TValue>>,IEnumerable<KeyValuePair<TKey,TValue>>,IEnumerable
     2 {
     3   void Add(TKeykey,TValuevalue);
     4   bool ContainsKey(TKeykey);
     5   bool Remove(TKeykey);
     6   bool TryGetValue(TKeykey,outTValuevalue);
     7 
     8   TValue this[TKeykey]{get;set;}
     9   ICollection<TKey> Keys{get;}
    10   ICollection<TValue> Values{get;}
    11 }
    1 public interface IList<T> : ICollection<T>,IEnumerable<T>,IEnumerable
    2 {
    3   int IndexOf(Titem);
    4   void Insert(intindex,Titem);
    5   void RemoveAt(intindex);
    6 
    7   T this[intindex]{get;set;}
    8 }

    IList 是直接继承自 ICollection 和 IEnumerable。
    所以它包括两者的功能,并且支持根据下标访问和添加元素。IndexOf, Insert, RemoveAt等等。
    可以说,IEnumerable 支持的功能最少,只有遍历。而 ICollection 支持的功能稍微多一点,不仅有遍历还有维护这个集合的功能。而 IList 是最全的版本。

    1 public class List<T> : IList<T>,ICollection<T>,IEnumerable<T>,IList,ICollection,IEnumerable
    2 public class Dictionary<TKey,TValue>:IDictionary<TKey,TValue>,ICollection<KeyValuePair<TKey,TValue>>,IEnumerable<KeyValuePair<TKey,TValue>>,IDictionary,ICollection,IEnumerable

    泛型 list 和泛型 Dictionary 都是继承自泛型 ICollection


    IReadOnlyList<T>
    这个是在 Framework4.5 中新增的接口类型。可以被看作是IList<T>的缩减版,去掉了所有可能更改这个集合的功能。比如:Add, RemoveAt 等等。

    泛型集合与非泛型集合的对应


    ArrayList    =>  List<T>
    Stack,Queue  =>  Stack<T>,Queue<T>  
    HashTable           =>  Dictionary<TKey,TValue>
    SortedList            =>  SortedList<TKey,TValue> / SortedDictionary<TKey,TValue>




    参考:http://www.cnblogs.com/C-CHERS/p/5809087.html



    转载请标明出处

    作者:AaXuan

    地址:http://www.cnblogs.com/Aaxuan

    知识共享许可协议

    本作品采用  知识共享署名 3.0 未本地化版本许可协议  进行许可。

  • 相关阅读:
    windows bat脚本检测空白文件
    linux把文件夹作为img镜像给rk3288烧录
    嵌入式非浮点型交叉编译器下载
    嵌入式linux date -s写入保存时间后,开机启动相差八小时
    android5.1编译 collect2: ld terminated with signal 9 [Killed]
    qt5.9.9交叉编译并运行到目标板完整版(无界面版本)
    嵌入式linux编译移植 vsftpd 源码修改
    嵌入式Linux板子date时间和hwclock不一致
    ubuntu14.0输入密码后进不去桌面
    C 语言 指针
  • 原文地址:https://www.cnblogs.com/Aaxuan/p/8644293.html
Copyright © 2011-2022 走看看