程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C# - ArrayList與Hashtable及其泛型版本

C# - ArrayList與Hashtable及其泛型版本

編輯:C#基礎知識

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;  
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved