Sunday, July 6, 2008

.NET Collections

System.Collections.IEnumerable
  • Exposes the enumerator, which supports a simple iteration over a non-generic collection.
  • Assembly: mscorlib, Version=3.5.0.0
  • Inherits nothing, this is the base interface.
  • Methods
    • Function GetEnumerator() As IEnumerator
      • Returns an enumerator that iterates through a collection.

System.Collections.ICollection

  • Defines size, enumerators, and synchronization methods for all non-generic collections.
  • Assembly: mscorlib, Version=3.5.0.0
  • Inherits IEnumerable
  • Methods
    • Sub CopyTo(ByVal array As Array, ByVal index As Integer)
      • Copies the elements of the ICollection to an Array.
  • IDictionary and IList are more specialized interfaces that extend ICollection.
  • If neither the IDictionary interface nor the IList interface meet the requirements of the required collection, derive the new collection class from the ICollection interface instead for more flexibility.
System.Collections.IDictionary
  • Represents a non-generic collection of key/value pairs.
  • Assembly: mscorlib, Version=3.5.0.0
  • Inherits ICollection, IEnumerable
  • Methods
    • Sub Add(ByVal key As Object, ByVal value As Object)
    • Sub Clear()
    • Function Contains(ByVal key As Object) As Boolean
    • Function GetEnumerator() As IDictionaryEnumerator
    • Sub Remove(ByVal key As Object)

System.Collections.IList

  • Represents a non-generic collection of objects that can be individually accessed by index.
  • Assembly: mscorlib, Version=3.5.0.0
  • Inherits ICollection, IEnumerable
  • Methods
    • Function Add(ByVal value As Object) As Integer
    • Sub Clear()
    • Function Contains(ByVal value As Object) As Boolean
    • Function IndexOf(ByVal value As Object) As Integer
    • Sub Insert(ByVal index As Integer, ByVal value As Object)
    • Sub Remove(ByVal value As Object)
    • Sub RemoveAt(ByVal index As Integer)

System.Collections.Hashtable

  • Represents a collection of key/value pairs that are organized based on the hash code of the key.
  • Assembly: mscorlib, Version=3.5.0.0
  • Implements IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable
  • Hashtable is thread safe for use by multiple reader threads and a single writing thread.

System.Collections.ArrayList

  • Implements the IList interface using an array whose size is dynamically increased as required.
  • Assembly: mscorlib, Version=3.5.0.0
  • Implements IList, ICollection, IEnumerable, ICloneable
  • Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

System.Collections.CollectionBase

  • Provides the abstract base class for a strongly typed collection.
  • Assembly: mscorlib, Version=3.5.0.0
  • Implements IList, ICollection, IEnumerable
  • Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

System.Collections.Specialized.StringDictionary

  • Implements a hash table with the key and the value strongly typed to be strings rather than objects.
  • Assembly: System, Version=3.5.0.0
  • Implements IEnumerable
  • Methods
    • Public Sub New()
      Public Overridable Sub Add(ByVal key As String, ByVal value As String)
    • Public Overridable Sub Clear()
    • Public Overridable Function ContainsKey(ByVal key As String) As Boolean
    • Public Overridable Function ContainsValue(ByVal value As String) As Boolean
    • Public Overridable Sub CopyTo(ByVal array As Array, ByVal index As Integer)
    • Public Overridable Function GetEnumerator() As IEnumerator
    • Public Overridable Sub Remove(ByVal key As String)
  • Instead of this class, you should use Dictionary<TKey, TValue> instead
  • Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Thread Safety

  • Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

Reference:

System.Collections Namespace

blog comments powered by Disqus