BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

Collection Interfaces

The thirty-sixth part of the C# Fundamentals tutorial introduces the use of collections in C#. A collection is similar to an array in that it contains a group of objects. However, the use of varying types of collection provide for more functionality.

IList.Item

The Item property provides direct access to any object within a collection using an index number. To specify the index number square brackets are used as with arrays. The following example creates and populates an ArrayList using the Add method. The value of one of the items in the collection is then read, modified and read again the index number.

ArrayList names = new ArrayList();

// Add some names
names.Add("Andrew");
names.Add("Alex");
names.Add("Adrienne");

Console.WriteLine(names[1]);
names[1] = "Alan";
Console.WriteLine(names[1]);

/* OUTPUT

Alex
Alan

*/

NB: This property is called the indexer for the IList collection.

IDictionary Interface

A dictionary is a collection that holds related key-value pairs. Each entry in the collection consists of two objects. The first, called the key, is a unique object that identifies an entry in the collection. The second is the value object. The value does not have to be unique within the dictionary.

The IDictionary interface is implemented by all dictionary collections. As with IList, IDictionary inherits ICollection so also supports all of the ICollection behaviour.

IDictionary.Add

The Add method in IDictionary is similar to the IList.Add method. It is used to add a new item to the dictionary. As dictionaries are identified by the specified key rather than an index number, no value is returned.

The following examples of IDictionary behaviours use the Hashtable class. This type of dictionary provides a fast method for retrieving items from the collection by hashing the key into a code that is used as an index.

Hashtable users = new Hashtable();

// Add some users
users.Add("andyb", "Andrew Brown");
users.Add("alexg", "Alex Green");
users.Add("adrienneb", "Adrienne Black");

Console.WriteLine(users.Count);                 // Outputs "3"

IDictionary.Remove

The Remove method removes an object from a dictionary, using the unique key as the only parameter. If the key does not exist within the collection the operation has no effect.

Hashtable users = new Hashtable();

// Add some users
users.Add("andyb", "Andrew Brown");
users.Add("alexg", "Alex Green");
users.Add("adrienneb", "Adrienne Black");
users.Remove("andyb");

Console.WriteLine(users.Count);                 // Outputs "2"

IDictionary.Clear

The Clear method deletes all items from a dictionary. This method is similar to the Clear method of the IList interface.

IDictionary.Contains

The Contains method provides similar functionality to IList.Contains. It returns a Boolean value indicating if a specific key exists within a dictionary.

Hashtable users = new Hashtable();

// Add some users
users.Add("andyb", "Andrew Brown");
users.Add("alexg", "Alex Green");
users.Add("adrienneb", "Adrienne Black");

Console.WriteLine(users.Contains("alexg"));       // Outputs "True"
Console.WriteLine(users.Contains("Alex Green"));  // Outputs "False"

IDictionary.IsFixedSize and IDictionary.IsReadOnly

In the description of the properties of the IList interface it was explained that collections can be of fixed size or can be read-only. This is true of dictionary collections so IDictionary provides the IsFixedSize and IsReadOnly properties; both return a Boolean value.

IDictionary.Item

The IDictionary interface defines an Item property as the indexer for dictionary classes. This allows individual items within the collection to be identified using an index provided in square brackets. In the case of dictionaries the key object is used rather than an index number.

Hashtable users = new Hashtable();

// Add some users
users.Add("andyb", "Andrew Brown");
users.Add("alexg", "Alex Green");
users.Add("adrienneb", "Adrienne Black");

Console.WriteLine(users["alexg"]);              // Outputs "Alex Green"
24 April 2007