Introduction to Collections in C#
Collections are similar to arrays allowing you to group together data, and providing a more
comprehensive set of methods for inserting and removing elements.
Collection classes
ArrayList a dynamic sized array that can contain any type of object BitArray a compact array of
bit values whose value can be either true or false HashTable a sorted list of key / value pairs that
can be accessed by their keys Queue a first in – first out collection SortedList a sorted list of key
value pairs that can be accessed using the keys or indexes Stack a list of objects in a last in – first
out manner StringCollection a string specific array type collection
The advantage of a collection over an array is that a collection can be dynamically resized.
All collections work in the same way although there may be some class specific ways to use
them.
The most useful collection is the ArrayList which allows you to access elements In the
collections just like an ordinary array but items can be added more easily.
It has a constructor that defines the number of elements to start with.
The following example illustrates using an ArrayList of boxes showing the three most important
methods of collections
● Add
● Remove
● Insert
Note that the example must have the System.Collections name space.
using System;
using System.Collections;
namespace collections
{
class Program
{
static void Main(string[] args)
{
ArrayList boxes = new ArrayList(5);
boxes.Add(new Box(12.3, 10.4, 3.6));
boxes.Add(new Box(11.3, 20.4, 5.6));
boxes.Add(new Box(13.3, 1.4, 1.6));
boxes.Add(new Box(1.3, 10.4, 6.6));
boxes.Add(new Box(132.3, 10.4, 10.6));
Console.WriteLine("---------------------------");
foreach (Box item in boxes)
{
Console.WriteLine("Volume {0}", item.Volume());
}
boxes.Remove(boxes[2]);
boxes.Remove(boxes[3]);
Console.WriteLine("---------------------------");
foreach (Box item in boxes)
{
Console.WriteLine("Volume {0}", item.Volume());
}
Console.WriteLine("---------------------------");
boxes.Add(new Box(11.3, 20.4, 5.6));
foreach (Box item in boxes)
{
Console.WriteLine("Volume {0}", item.Volume());
}
Console.WriteLine("---------------------------");
boxes.Insert(2, new Box(3,4,5));
foreach (Box item in boxes)
{
Console.WriteLine("Volume {0}", item.Volume());
}
}
}
public class Box
{
private double height;
private double width;
private double depth;
public Box()
{
height = 10;
width = 10;
depth = 10;
}
public Box(double h, double w, double d)
{
height = h;
width = w;
depth = d;
}
public double Height
{
get
{
return Height;
}
set
{
if (value <= 0)
{
height = 10;
}
else
{
height = value;
}
}
}
public double Width
{
get
{
return width;
}
set
{
if (value <= 0)
{
width = 10;
}
else
{
width = value;
}
}
}
public double Depth
{
get
{
return Depth;
}
set
{
if (value <= 0)
{
depth = 10;
}
else
{
depth = value;
}
}
}
public double Volume()
{
return height * width * depth;
}
In this example we started with 5 elements and then dynamically changed the number of
elements. This demonstrates the usefulness of collections in programming.
Note that the C# built-in collection classes allow you to add any type of object to any type of
collection, including simple data types.
Building a Custom Collection Class
Sometimes you want additional control over a collection. You may want a collection that only
works with a particular class type, or you may want to define other processing in the collection.
The .net framework provides a class called CollectionBase which provides base methods for a
strongly typed collection.
To use this, we inherit from this class to create our own collection class.
public class BoxCollection : CollectionBase
{
public Box this[int index]
{
get
{
return (Box)List[index];
}
set
{
List[index] = value;
}
}
public int Add(Box value)
{
return List.Add(value);
}
public int IndexOf(Box value)
{
return List.IndexOf(value);
}
public void Insert(int index, Box value)
{
List.Insert(index, value);
}
public void Remove(Box value)
{
List.Remove(value);
}
public bool Contains(Box value)
{
//returns false if value is not of type Box
return List.Contains(value);
}
protected override void OnInsert(int index, Object value)
{
//code which is run when inserting values
}
protected override void OnRemove(int index, Object value)
{
//code which is run when removing values
}
protected override void OnSet(int index, Object oldValue, Object newValue)
{
//code which is run when setting values
}
The above example demonstrates how you can implement the common methods and override
some methods such as
● OnInsert
● OnRemove
● OnSet
● OnValidate
These methods are called automatically when the Add. Insert, Remove and son on events are
called so you don’t have to call them yourself.
Note that an indexer has been added to the class public Box this[int index] to allow you to access
the elements of the collection using array syntax.