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.Insert

The Insert method is similar to Add as it allows new items to be added to the collection. With Insert the index of the new item is specified. The object is inserted at the specified index and existing objects are moved to a higher index to make space. If the index number specified is too large, an ArgumentOutOfRangeException is thrown.

ArrayList names = new ArrayList();

// Add some names
names.Add("Andrew");
names.Add("Alex");
names.Insert(0, "Adrienne");                    // Insert at start of collection

foreach(string name in names)
{
    Console.WriteLine(name);
}

/* OUTPUT

Adrienne
Andrew
Alex

*/

IList.RemoveAt

The RemoveAt method removes an item from the collection using the index number to identify the object to delete. Any items in the collection with a higher index are moved to ensure that the indexes remain contiguous.

ArrayList names = new ArrayList();

// Add some names
names.Add("Andrew");
names.Add("Alex");
names.Add("Adrienne");
names.RemoveAt(1);                              // Remove the second item

foreach(string name in names)
{
    Console.WriteLine(name);
}

/* OUTPUT

Andrew
Adrienne

*/

IList.Clear

The Clear method is the last IList method used to modify the contents of a collection. It removes all of the items from the collection.

ArrayList names = new ArrayList();

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

Console.WriteLine(names.Count);                 // Outputs "0"

IList.Contains

Sometimes it is necessary to determine if an object exists within a collection. The Contains method can be used for this purpose by passing the object to find as the only parameter. The method returns true if the item is present and false otherwise.

ArrayList names = new ArrayList();

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

Console.WriteLine(names.Contains("Alex"));      // Outputs "True"
Console.WriteLine(names.Contains("Albert"));    // Outputs "False"

IList.IndexOf

The final method of the IList interface is IndexOf. This method is similar to the Contains method as it is used to determine if an object is present within a collection. If the object exists, its index number is returned. If the object does not exist, the return value is -1.

ArrayList names = new ArrayList();

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

Console.WriteLine(names.IndexOf("Alex"));       // Outputs "1"
Console.WriteLine(names.IndexOf("Brian"));      // Outputs "-1"

IList.IsFixedSize

Although most collections provide a dynamic group of items, it is possible to have fixed size collections. A fixed size collection may be modified but new items may not be added and existing items may not be removed. If either operation is attempted, a NotSupportedException is thrown. To check if a collection is fixed in size, read the Boolean IsFixedSize property.

ArrayList names = new ArrayList();

Console.WriteLine(names.IsFixedSize);           // Outputs "False"

IList.IsReadOnly

One step further than having a fixed size collection is a read-only collection. Such a list may not be modified in any way. To check if a collection is read-only, read the IsReadOnly property.

ArrayList names = new ArrayList();

Console.WriteLine(names.IsReadOnly);            // Outputs "False"

NB: Fixed size and read-only collections are generally wrappers for existing collections that restrict write access. If the underlying collection is modified the restricted version mirrors the changes without throwing an exception.

24 April 2007