您的位置:首页 - 教程 - C# - 正文
C# - ArrayList与Hashtable及其泛型版本

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表示键/值对的非泛型集合

public interface IDictionary : ICollection, IEnumerable {
	bool IsReadOnly { get; }
	object this[int index] { get; set; } 
	ICollection Keys { get; }
	ICollection Values { get; }

    void Add(object key, object value);
	void Remove(object key);
	void Clear();
	bool Contains(object key);
	IDictionaryEnumerator GetEnumerator();
}

其中,IDictionaryEnumerator表示非泛型字典集的枚举器

public interface IDictionaryEnumerator : IEnumerator {
	DictionaryEntry Entry { get; }
    object Key { get; }
    object Value { get; }
}

其中,DictionaryEntry表示字典集的元素(键/值对)  

public struct DictionaryEntry {
	public DictionaryEntry(object key, object value);
	public object Key { get; set; }
	public object Value { get; set; }
}

Dictionary<TKey, TValue>:键值对的泛型集合  

public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback {
	public int Count { get; }
    public Dictionary<TKey, TValue>.KeyCollection Keys { get; }
    public Dictionary<TKey, TValue>.ValueCollection Values { get; }
    public TValue this[TKey key] { get; set; }

    public Dictionary();
	public Dictionary(int capacity);
	public Dictionary(IDictionary<TKey, TValue> dictionary);
	public Dictionary<TKey, TValue>.Enumerator GetEnumerator(); // 枚举器
	public void Add(TKey key, TValue value);
	public bool Remove(TKey key);
    public void Clear();
    public bool ContainsKey(TKey key);
    public bool ContainsValue(TValue value);   
    public bool TryGetValue(TKey key, out TValue value);

    public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>, IDisposable, IDictionaryEnumerator, IEnumerator {}
    public sealed class KeyCollection : ICollection<TKey>, IEnumerable<TKey>, ICollection, IEnumerable {}
    public sealed class ValueCollection : ICollection<TValue>, IEnumerable<TValue>, ICollection, IEnumerable {}
}

其中,接口IDictionary<TKey, TValue>表示键/值对的泛型集合  

public interface IDictionary<TKey, TValue> : ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable {
	TValue this[TKey key] { get; set; }
	ICollection<TKey> Keys { get; }
    ICollection<TValue> Values { get; }

    void Add(TKey key, TValue value);
    bool Remove(TKey key);
    bool ContainsKey(TKey key);
    bool TryGetValue(TKey key, out TValue value);
} 

总结

  • 单线程程序中推荐使用Dictionary,多线程程序中推荐使用Hashtable;  

评论: