Visual C# 2005 Guide
Sets (Arraylist)

A set is a collection of values of the same type. The values in a set do not have any particular order. Some programming languages (like Pascal) have this data structure built it. C# 2005 does not currently have the type but future versions of the .NET framework are intended to. For now, the nearest thing to it that we can use is called the ArrayList class.

The Set functionality that we are interested in emulating with the ArrayList is the ability to create an unsorted list of values, add other lists to that list and then to check if values belong to the set.

The following program creates a set (as an ArrayList), adds values to it and checks to see if a specific value is a member of the set. Before you run the program, look at the top of the screen. Change the line that says using System.Collections.Generic; so that it reads simply, using System.Collections; We do this to tell the compiler we want to use the part of the .NET class library that contains the ArrayList.

ArrayList Set1 = new ArrayList();
int[] rangeToAdd = { 5, 8, 9 };
Set1.Add(3);
Set1.AddRange(rangeToAdd);
if (Set1.Contains(8))
{
   Console.WriteLine("8 is in the set");
}
Console.ReadLine();

For now just monkey about with the code and see it work. We'll come back to the applications of this when we have a bit more C# under our belts.