对C# 集合类的总结

集合类
在mscorlib程序集下面System.Collections命名空间下的 是关于集合的接口以及系统集合类的一些定义
    public interface ICollection : IEnumerable
public interface IComparer
public interface IDictionary : ICollection, IEnumerable
public interface IDictionaryEnumerator : IEnumerato
public interface IEnumerable
public interface IEnumerator
public interface IEqualityComparer
public interface IHashCodeProvider
public interface IList : ICollection, IEnumerable
internal class KeyValuePairs
     public interface IEnumerator   //枚举接口
     {
        bool MoveNext();
        object Current { get; }
        void Reset();
     }

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

           这个是集合类必须实现的基本方法。

           

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

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

          

     public interface ICollection : IEnumerable  //有序集合
    {
    // Methods
    void CopyTo(Array array, int index); //把集合对象拷贝

    // Properties
    int Count { get; }//集合的元素数目
    bool IsSynchronized { get; }  //返回集合是否是多线程同步访问
    object SyncRoot { get; }      //返回同步对象
    }
public interface IList : ICollection, IEnumerable //索引集合
   {
    // Methods
    int Add(object value);
    void Clear();
    bool Contains(object value);
    int IndexOf(object value);
    void Insert(int index, object value);
    void Remove(object value);
    void RemoveAt(int index);

    // Properties
    bool IsFixedSize { get; }
    bool IsReadOnly { get; }
    object this[int index] { get; set; }
   }

这个接口就很丰富了,有增加成员,删除成员等一些方法。
 
public interface IDictionary : ICollection, IEnumerable  //字典集合或键值集合
{
    // Methods
    void Add(object key, object value);
    void Clear();
    bool Contains(object key);
    IDictionaryEnumerator GetEnumerator();
    void Remove(object key);

    // Properties
    bool IsFixedSize { get; }
    bool IsReadOnly { get; }
    object this[object key] { get; set; }
    ICollection Keys { get; }
    ICollection Values { get; }
}
 

      集合类型都实现了IEnumerable接口,从而可以使用foreach迭代。

   实现了ICollectoin接口的集合类表明集合中的元素是有先后顺序的。

   IList接口继承了ICollection接口,实现了IList接口的集合类不止表明集合中的元素是有先后顺序,而且表明集合类可以通过下标访问的方式访问集合元素而且下标是整数。

   IDictionary接口也继承了ICollection接口,实现了IDicionary接口的集合类可以通过下标key访问的访问方式访问集合元素。

          ICollection 接口是 System.Collections 命名空间中类的基接口。

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

          某些集合(如 Queue 类和 Stack 类)限制对其元素的访问,它们直接实现 ICollection 接口。

          如果 IDictionary 接口和 IList 接口都不能满足所需集合的要求,则从 ICollection 接口派生新集合类以提高灵活性









在此ILIST 接口的基础上产生一些类型是我们实际编程中能用的到的比如
  1: public abstract class Array : ICloneable, IList, ICollection, IEnumerable //数组类 这个类是抽象的
我们平时用到的比如 int[] intarray= new int[]{}是派生于这个抽取类的
但是数组是从Array隐式派生的,这是由编译器完成的,所以我们看不到具体的实现过程。
比如

         Array array1;
         int[] a = new int[] { };
         string[] b = new string[] { };
         array1 = a;
         array1 = b

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

    public class ArrayList : IList, ICollection, IEnumerable, ICloneable
object 可以传入任何类型 但是涉及装箱和拆箱的操作性能受影响。
3: Stack 和 Queue 栈和队列 是有序集合.
4: HashTable
HashTable实现了IDictionary接口。是键值对的集合
也是键和值都是object 但是索引集合的下标是int ,value是object
但是 hashtable 键值不是按照插入的顺序进行排列。
4:
        public class SortedList : IDictionary, ICollection, IEnumerable, ICloneable
    sortlist  也是字典类但是按照顺序排放的类



非范性的集合类就这些了。
范性集合类
命名空间下的 System.Collections.Generic
System.Collections.Generic是.NET 2.0新增的一个命名空间。C#1中的集合类型都存在着装箱拆箱的性能问题以及潜在的类型安全问题,
丑陋的设计必须得到改进,
于是C#2就引入了泛型这个概念。
泛型即解决了装箱拆箱的性能问题,又解决了潜在的类型转换安全问题,所以在实际应用中,推荐使用泛型集合代替非泛型集合。




基础接口
    public interface IEnumerator<T> : IDisposable, IEnumerator  // 这个范性接口继承自非范性的同名接口,
    {
    // Properties
         T Current { get; }
    }
     public interface IEnumerable<T> : IEnumerable // 
     {
    // Methods
        IEnumerator<T> GetEnumerator();
     }

Collection 接口就完全不一样了增加了许多的方法。说明微软的范性集合类是新的一套东西

     public interface ICollection<T> : IEnumerable<T>, IEnumerable
     {
        // Methods
      void Add(T item);
      void Clear();
      bool Contains(T item);
      void CopyTo(T[] array, int arrayIndex);
      bool Remove(T item);

      // Properties
      int Count { get; }
      bool IsReadOnly { get; }
   }
    public interface IDictionary<TKey, TValue> : ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable
   {
    // Methods
    void Add(TKey key, TValue value);
    bool ContainsKey(TKey key);
    bool Remove(TKey key);
    bool TryGetValue(TKey key, out TValue value);

    // Properties
    TValue this[TKey key] { get; set; }
    ICollection<TKey> Keys { get; }
    ICollection<TValue> Values { get; }
   }
   public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
   {
    // Methods
    int IndexOf(T item);
    void Insert(int index, T item);
    void RemoveAt(int index);

    // Properties
    T this[int index] { get; set; }
   }
范性list和Dictionary都是继承自范性Coleection
   public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
   public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable
  c# 在范性里发明了LIST范性集合
泛型集合与非泛型集合的对应 

      ArrayList => List<T>  新的泛型集合去掉了Array前缀。

  Stack,Queue => Stack<T>,Queue<T>

  HashTable => Dictionary<TKey,TValue> 新的泛型键值集合的命名放弃了HashTable,但其内部实现原理还是和HashTable有很大相似的。

  SortedList => SortedList<TKey,TValue> | SortedDictionary<TKey,TValue>







  












 
   




菜鸟-潜水中
原文地址:https://www.cnblogs.com/C-CHERS/p/5809087.html