Collections Tutorial
Use collections with C#


Date: 2002-April
Author: (luKa)
Environment: .NET Framework v1.0

Download Examples

Selecting a collection

All the collections implement IEnumerable interface that's extended by ICollection. There are two interfaces for collections derived from ICollection: IDictionary and IList.

Gerarchia delle classi

IList interface is for collections of values. Here is the list of collections that implement it:
The interface IDictionary is for collections of (Key, Value) pairs. Here is the list of collections that implement it:
Other collections derived from ICollection are:
Then there is System.Collections.Specialized.StringDictionary that implements just IEnumerable and there is also System.Collections.Specialized.BitVector32 tha's a collection of bit/Boolean.

See:
    Microsoft .NET Framework SDK Documentation - .NET Framework Developer's Guide - Selecting a Collection Class     (local)

Iterating collection items

foreach, IEnumerator and IEnumerable

foreach statement let iterate through collection items.
See:
    Microsoft .NET Framework SDK Documentation - C# Programmer's Reference - foreach, in     (local)
For IEnumerable see:
    MS .NET Framework SDK Doc - .NET Framework Class Library - IEnumerable Interface     (local)
For IEnumerator see:
    MS .NET Framework SDK Doc - .NET Framework Class Library - IEnumerator Interface     (local)

Download examples and see FOREACH.CS.

Strong-Typed collections

In C# it's possible to implement a type-safe iterator for a type-safe collection. A type-safe collection enable compiler to catch more errors at compile-type and avoid overhead of boxing/unboxing for Valur-Type items.
See:
    MS .NET Framework SDK Doc - C# Programmer's Reference - Collection Classes Tutorial     (local)
See the strong-typed collection StringCollection:
    MS .NET Framework SDK Doc - .NET Framework Class Library - StringEnumerator Class     (local)

See also the example FOREACH_STRONGTYPED.CS.

Define collection sorting

Some collection let specify sorting criteria for items iteration. Here is how sorting criteria can be specified:
1 - See GetHashCode() for Key elements of HashTable collection:
MS .NET Framework SDK Doc - .NET Framework Class Lib - Object.GetHashCode Method     (local)

2 - See interface IHashCodeProvider for Key elements of HashTable and NameValueCollection collection:
MS .NET Framework SDK Doc - .NET Framework Class Lib - IHashCodeProvider Interface     (local)

3 - See IComparable interface implemented by System.Array (affected methods are BinarySearch and Sort), ArrayList (affected methods are BinarySearch and Sort) and SortedList:
MS .NET Framework SDK Doc - .NET Framework Class Lib - IComparable Interface     (local)

4 - See IComparer interface to specify more alternative sorting criteria for the collections System.Array (affected methods are BinarySearch and Sort), >ArrayList (affected methods are BinarySearch and Sort), HashTable (for Key elements), SortedList, ListDictionary and NameValueCollection:
MS .NET Framework SDK Doc - .NET Framework Class Lib - IComparer Interface     (local)

GetHashCode method, IComparable implementation, Equals override and equality operator == must all be defined coherently. See:
MS .NET Framework SDK Doc - .NET Framework General Reference - Guidelines for Implementing Equals and the Equality Operator (==)     (local)

See HASH.CS example for a GetHashCode() implementation.
See following examples:
- SORT0.CS show that IComparable is necessary for sorting
- SORT1.CS show an IComparable implementation.
- SORT2.CS show an implementation of Equals that's coherent with the corrisponding IComparable implementation.
- SORT3.CS show an implementation of GetHashCode() that's coherent with the corrisponding Equals implementation.
- SORT4.CS show an implementation of the Equality Operator ==() that's coherent with the corrisponding Equals implementation.
- SORT5.CS implements an alternative sorting criteria with IComparer.