Namespace: System.Collections
Types of Collections:
Class
|
Description
|
ArrayList
|
Implements the IList interface using an
array whose size is dynamically increased as required.
|
BitArray
|
Manages a compact array of bit values, which
are represented as Booleans, where true indicates
that the bit is on (1) and false indicates the bit
is off (0).
|
CaseInsensitiveComparer
|
Compares two objects for equivalence,
ignoring the case of strings.
|
CaseInsensitiveHashCodeProvider
|
Obsolete. Supplies
a hash code for an object, using a hashing algorithm that ignores the case of
strings.
|
CollectionBase
|
Provides the abstract
base class for a strongly typed collection.
|
Comparer
|
Compares two objects for equivalence, where
string comparisons are case-sensitive.
|
DictionaryBase
|
Provides the abstract
base class for a strongly typed collection of key/value pairs.
|
Hashtable
|
Represents a collection of key/value pairs
that are organized based on the hash code of the key.
|
Queue
|
Represents a first-in, first-out collection
of objects.
|
ReadOnlyCollectionBase
|
Provides the abstract
base class for a strongly typed non-generic read-only collection.
|
SortedList
|
Represents a collection of key/value pairs
that are sorted by the keys and are accessible by key and by index.
|
Stack
|
Represents a simple last-in-first-out (LIFO)
non-generic collection of objects.
|
Intefaces:
Interface
|
Description
|
ICollection
|
Defines size, enumerators, and
synchronization methods for all nongeneric collections.
|
IComparer
|
Exposes a method that compares two objects.
|
IDictionary
|
Represents a nongeneric collection of
key/value pairs.
|
IDictionaryEnumerator
|
Enumerates the elements of a nongeneric
dictionary.
|
IEnumerable
|
Exposes the enumerator, which supports a
simple iteration over a non-generic collection.
|
IEnumerator
|
Supports a simple iteration over a
nongeneric collection.
|
IEqualityComparer
|
Defines methods to support the comparison of
objects for equality.
|
IHashCodeProvider
|
Obsolete. Supplies
a hash code for an object, using a custom hash function.
|
IList
|
Represents a non-generic collection of
objects that can be individually accessed by index.
|
ArrayList
The ArrayList class is a simple,
unordered container for objects of any type.
Add, AddRange(To add items from another collection or
ArrayList),
Array List supports the IEnumerable interface
that exposes GetEnumerator method that return IEnumerator interface.
IEnumerator have a property called Current and two methods including MoveNext
and Reset.
ArrayList collection = new
ArrayList();
IEnumerator e =
collection.GetEnumerator();
While(e.MoveNext())
{
Console.WriteLine(e.Current);
}
OR
foreach(object o in collection)
Console.WriteLine(o.ToString());
Consistent Interfaces in Collections:
Like IEnumerable interface, all
collection classes inherit from ICollection interface that intern inherit from
IEnumerable interface. ICollection interface provide you the following
Proepries:
Name
|
Description
|
Count
|
Gets the number of elements contained in the
ICollection.
|
IsSynchronized
|
Gets a value indicating whether access to
the ICollection is synchronized (thread safe).
|
SyncRoot
|
Gets an object that can be used to
synchronize access to the ICollection.
|
Important Methods:
Name
|
Description
|
CopyTo
|
Copies the elements of the ICollection to an
Array, starting at a particular Array index.
|
GetEnumerator
|
Returns an enumerator that iterates through
a collection. (Inherited from IEnumerable.)
|
Some simple list type collections inherit from IList
interface that intern inherit from ICollection interface.
IList Interface’s most important properties are:
Name
|
Description
|
Count
|
Gets the number of elements contained in the
ICollection. (Inherited from ICollection.)
|
IsFixedSize
|
Gets a value indicating whether the IList
has a fixed size.
|
IsReadOnly
|
Gets a value indicating whether the IList is
read-only.
|
IsSynchronized
|
Gets a value indicating whether access to
the ICollection is synchronized (thread safe). (Inherited from ICollection.)
|
Item
|
Gets or sets the element at the specified
index.
|
SyncRoot
|
Gets an object that can be used to
synchronize access to the ICollection. (Inherited from ICollection.)
|
IList Interface’s most important Methods are:
Name
|
Description
|
Add
|
Adds an item to the IList.
|
Clear
|
Removes all items from the IList.
|
Contains
|
Determines whether the IList contains a specific value.
|
CopyTo
|
Copies the elements of the ICollection to an Array, starting at a particular Array index. (Inherited from ICollection.)
|
GetEnumerator
|
Returns an enumerator that
iterates through a collection. (Inherited from IEnumerable.)
|
IndexOf
|
Determines the index of a
specific item in the IList.
|
Insert
|
Inserts an item to the IList at the specified index.
|
Remove
|
Removes the first occurrence
of a specific object from the IList.
|
RemoveAt
|
Removes the IList item at the specified index.
|
Inheritance Sequence:
IEnumerator → IEnumerable → ICollection → IList
Sorting Items:
To
sort item in an ArrayList, simply call ArrayList.Sort method. This method works
using Comparer class. Comparer is the default implementation of the ICompare interface. One can specify any other Compare class that implements the IComparer interface. Like another built-in implementation that perform Case Insensitive comparison i.e. CaseInsensitiveComparer.
Comments