C #-ArrayList and Hashtable and their generic versions

Source: Internet
Author: User

The collection class inheritance relationship for C # UML diagram:

Icollection-icollection<t>

ICollection: Size, enumerator, and synchronization methods for all non-generic collections

public interface Icollection:ienumerable {int Count {get;} BOOL issynchronized {get;}//Access to ICollection is synchronous (thread safe) object SyncRoot {get;}//Gets the object that can be used to synchronize access to ICollection void CopyTo (array array, int index);}

icollection<t>: Property Methods for generic collections

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: Implements the IList interface with an array that can be dynamically incremented on demand

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 ();p ublic ArrayList (int capacity);p ublic ArrayList (ICollection c);p ublic virtual IEnumerator GetEnumerator ([int idx, int cnt]); Enumerator public virtual object Clone (); Create a shallow copy of the ArrayList public virtual ArrayList GetRange (int idx, int cnt); Subset public virtual void SetRange (int idx, ICollection c); Set the value of ArrayList public static ArrayList ReadOnly (ArrayList list); Returns a read-only ArrayList wrapper public static IList ReadOnly (IList list); Returns the read-only IList wrapper public static ArrayList Synchronized (ArrayList list); Returns the thread-synchronized ArrayList wrapper public static IList Synchronized (IList list);  Returns the IList wrapper public static ArrayList Adapter (IList list) for thread synchronization; Return to IList ArrayList wrapper public virtual INT Add (object val);p ublic virtual void AddRange (ICollection c);p ublic virtual void Insert (int idx, object val);p ublic Virt UAL void Insertrange (int idx, ICollection c);p ublic virtual void Remove (object obj);p ublic virtual void RemoveAt (int idx); public virtual void RemoveRange (int idx, int cnt);p ublic virtual void Clear ();p ublic virtual bool Contains (object item);p u Blic virtual int IndexOf (object val [, int startidx, int cnt]);p ublic virtual int LastIndexOf (object val [, int startidx, int cnt]);p ublic virtual void Reverse ([int idx, int cnt]);       Reverse public virtual void Sort ([IComparer CMP]); Sort public virtual int BinarySearch (object val [, IComparer CMP]); Binary find public virtual object[] ToArray ();p ublic virtual void CopyTo (array array [, int arrayidx]);}

Where the interface IList represents a non-generic collection of objects that can be accessed individually by index

Public interface Ilist:icollection, IEnumerable {bool isreadonly {get;} Object This[int index] {get; set;}  Gets or sets the element at the specified index    , 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>:

where the interface IList<t> represents a collection of a set of objects that can be accessed individually by index

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: A collection of key/value pairs organized by the hash code of the key

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;}  Returns a IComparer object that is used to compare public Hashtable ();p ublic Hashtable (int capacity);p ublic Hashtable (IDictionary d);p ublic virtual IDictionaryEnumerator GetEnumerator (); Enumerator public virtual object Clone (); Create a shallow copy of the Hashtable public static Hashtable Synchronized (Hashtable table); Returns the thread-synchronized Hashtable wrapper public virtual void Add (object key, Object value);p ublic virtual void Remove (object key);p ublic Virtu Al void Clear ();p ublic virtual bool Contains (object key);p ublic virtual bool ContainsKey (object key);p ublic virtual bool C Ontainsvalue (object value);p rotected virtual int Gethash (object key);p rotected virtual bool Keyequals (object item, object key); Compared to the key public virtual void CopyTo (array array, int arrayindex);}

Where interface IDictionary represents a non-generic collection of key/value pairs

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 ();}

Where IDictionaryEnumerator represents an enumerator for a non-generic dictionary set

Public interface Idictionaryenumerator:ienumerator {dictionaryentry Entry {get;}    Object Key {get;}    Object Value {get;}}

Where dictionaryentry represents the elements of a dictionary set (key/value pairs)

public struct DictionaryEntry {public DictionaryEntry (object key, Object value);p Ublic object key {get; set;} Public object Value {get; set;}}

Dictionary<tkey, Tvalue> : A generic collection of key-value pairs

public class Dictionary<tkey, tvalue>: Idictionary<tkey, Tvalue>, Icollection<keyvaluepair<tkey, Tvalue>&gt, 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 ();p ublic Dictionary (int capacity);p ublic Dictionary (Idictionary<tkey, tvalue> Dictionary); Public Dictionary<tkey, Tvalue>. Enumerator GetEnumerator ();    Enumerator public void Add (TKey key, TValue value);p ublic 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&LT;TKEY&G t;, ICollection, IEnumerable {} public sealed class Valuecollection:icollection<tvalue>, ienumerable<tvalue& gt;, ICollection, IEnumerable {}}

Where interface Idictionary<tkey, tvalue> represents a generic collection of key-value pairs  

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);}

Summary :

    • It is recommended to use dictionary in single-threaded program, Hashtable is recommended in multithreaded programs;

C #-ArrayList and Hashtable and their generic versions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.