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.

ICollection.Count

The Count property returns the number of items that exist within a collection as an integer value.

ArrayList names = new ArrayList();

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

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

ICollection.IsSynchronized

When writing programs that are multi-threaded, it is possible that more than one thread of execution could access a collection simultaneously. If many threads are reading from and writing to a collection, unexpected problems could occur. To alleviate this problem collections may be synchronised. A synchronised (or synchronized) collection allows writing from multiple threads.

To determine if a collection is synchronised, you can check the Boolean IsSynchronized property. By default, collections are not synchronised.

ArrayList names = new ArrayList();

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

ICollection.SyncRoot

The SyncRoot property returns an object that may be used for synchronisation for thread-safety. Even when a collection is synchronised there are situations where it is not thread-safe. For example, when looping, or enumerating, through a collection's items another thread may modify the collection. This causes an exception to be thrown. To guarantee thread-safety in this situation, the exceptions can be caught and handled or the collection may be locked using the SyncRoot object. Whilst locked, no other threads may access the collection.

The following example demonstrated how a collection may be locked. The collection remains unavailable to other threads until the end of the code block associated with the lock statement.

ArrayList names = new ArrayList();

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

lock (names.SyncRoot)                           // Lock the collection
{
    foreach(string name in names)
    {
        Console.WriteLine(name);
    }
}                                               // Collection now unlocked

/* OUTPUT

Andrew
Alex
Adrienne

*/

IList Interface

The IList interface declares the properties and methods of a collection that represents a list of numbered items. Each item in a list has a zero-based index that is similar to an array's index. It is not coincidental that arrays have this behaviour as they also implement the IList interface.

The members of the IList interface are described below. Note that not all collections support every behaviour. Where a method or property is not supported, calling it throws a NotSupportedException. The IList interface is derived from, or inherits, ICollection. This means that all collections that support the IList behaviour also implement ICollection.

IList.Add

The Add method adds a new object to the collection; the object to add being passed to the only parameter. The method returns an integer value indicating the index position at which the new object was inserted. Remember that the index is zero-based so the first entry is at position zero.

ArrayList names = new ArrayList();

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

Console.WriteLine(position);                    // Outputs "2"

IList.Remove

The Remove method removes an object from a collection, using the object itself as a lookup parameter. If the object has multiple occurrences within the list, only the first match is removed. If the object does not exist within the collection the operation has no effect.

ArrayList names = new ArrayList();

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

Console.WriteLine(names.Count);                 // Outputs "2"
24 April 2007