ArrayList与Hashtable及其泛型版本

时间:2022-03-11 19:50:10

C#的集合类继承关系UML图:

ICollection - ICollection<T>

ICollection:所有非泛型集合的大小、枚举器和同步方法

public interface ICollection : IEnumerable { int Count { get; } bool IsSynchronized { get; } // 对ICollection的访问是否是同步的(线程安全) object SyncRoot { get; } // 获取可用于对ICollection同步访问的对象 void CopyTo(Array array, int index); }

ICollection<T>:泛型集合的属性方法

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

ArrayList - List<T>

ArrayList: 使用大小可按需动态增加的数组实现IList接口

public class ArrayList : IList, ICollection, IEnumerable, ICloneable { public virtual int Capacity { get; set; } public virtual int Count { get; } public virtual bool IsReadOnly { get; } public virtual bool IsSynchronized { get; } public virtual object SyncRoot { get; } public virtual object this[int index] { get; set; } public ArrayList(); public ArrayList(int capacity); public ArrayList(ICollection c); public virtual IEnumerator GetEnumerator([int idx, int cnt]); // 枚举器 public virtual object Clone(); // 创建ArrayList的浅表副本 public virtual ArrayList GetRange(int idx, int cnt); // 子集 public virtual void SetRange(int idx, ICollection c); // 设置ArrayList的值 public static ArrayList ReadOnly(ArrayList list); // 返回只读的ArrayList包装 public static IList ReadOnly(IList list); // 返回只读的IList包装 public static ArrayList Synchronized(ArrayList list); // 返回线程同步的ArrayList包装 public static IList Synchronized(IList list); // 返回线程同步的IList包装 public static ArrayList Adapter(IList list); // 返回IList的ArrayList包装 public virtual int Add(object val); public virtual void AddRange(ICollection c); public virtual void Insert(int idx, object val); public virtual void InsertRange(int idx, ICollection c); public virtual void Remove(object obj); public virtual void RemoveAt(int idx); public virtual void RemoveRange(int idx, int cnt); public virtual void Clear(); public virtual bool Contains(object item); public virtual int IndexOf(object val [, int startIdx, int cnt]); public virtual int LastIndexOf(object val [, int startIdx, int cnt]); public virtual void Reverse([int idx, int cnt]); // 反转 public virtual void Sort([IComparer cmp]); // 排序 public virtual int BinarySearch(object val [, IComparer cmp]); // 二分查找 public virtual object[] ToArray(); public virtual void CopyTo(Array array [, int arrayIdx]); }

其中,接口IList表示对象的非泛型集合,可按照索引单独访问  

public interface IList : ICollection, IEnumerable { bool IsReadOnly { get; } object this[int index] { get; set; } // 获取或设置指定索引处的元素 int Add(object value); void Insert(int index, object value); void Remove(object value); void RemoveAt(int index); void Clear(); bool Contains(object value); int IndexOf(object value); }

List<T>:  

其中,接口IList<T>表示可按照索引单独访问的一组对象的集合 

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable { T this[int index] { get; set; } void Insert(int index, T item); void RemoveAt(int index); int IndexOf(T item); }  

Hashtable - Dictionary<TKey, TValue>

Hashtable:根据键的哈希代码进行组织的键/值对集合

public class Hashtable : IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable { public virtual int Count { get; } public virtual bool IsReadOnly { get; } public virtual bool IsSynchronized { get; } public virtual object SyncRoot { get; } public virtual ICollection Keys { get; } public virtual ICollection Values { get; } public virtual object this[object key] { get; set; } protected IComparer comparer { get; set; } // 返回IComparer对象,用于比较 public Hashtable(); public Hashtable(int capacity); public Hashtable(IDictionary d); public virtual IDictionaryEnumerator GetEnumerator(); // 枚举器 public virtual object Clone(); // 创建Hashtable的浅表副本 public static Hashtable Synchronized(Hashtable table); // 返回线程同步的Hashtable包装 public virtual void Add(object key, object value); public virtual void Remove(object key); public virtual void Clear(); public virtual bool Contains(object key); public virtual bool ContainsKey(object key); public virtual bool ContainsValue(object value); protected virtual int GetHash(object key); protected virtual bool KeyEquals(object item, object key); // 与键比较 public virtual void CopyTo(Array array, int arrayIndex); }

其中,接口IDictionary表示键/值对的非泛型集合