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.

What is a Collection?

A collection in C# is a group of related objects held in a structure. There are many types of collection defined within the .NET framework, each providing a different structure of objects. These include collections for simple lists, queues and stacks.

A collection is often compared to an array. Both store groups of related objects. However, there are some key differences. For example, collections are one-dimensional unlike arrays, which may have many dimensions. Arrays tend to be fixed in size whereas collections can vary in length and may allow insertion at any position within the set.

One of the most important benefits provided by collections is that they remove the need to develop common structures and algorithms. These include the already mentioned queue and stack, sorted lists, bit arrays and associative arrays, also known as dictionaries or collections of key-value pairs.

Boxing and Unboxing

.NET 1.1 Collections are used to hold groups of objects. This means that value types and structures cannot be natively held in a collection. However, it is possible to hold a value or structure in an object using the concept of boxing. When a value is assigned to an object, the value is boxed and behaves like a reference type object. The object can later be converted back to a value, or unboxed, by casting it to its original data type.

The following example demonstrates a value being boxed and unboxed.

int valueToBox = 123;
object boxed = valueToBox;                      // Boxed
int valueOutOfBox = (int)boxed;                 // Unboxed

Console.WriteLine(valueOutOfBox);               // Outputs "123"

NB: When adding value types and structures to a collection, boxing occurs automatically. When retrieving a value from a collection, it must be unboxed using the cast operator.

Collection Types

There are three main types of collection provided by the .NET framework:

  • General-Purpose. The general-purpose collections are used to provide standard data structures that hold any type of object. These include the dynamically sized array, stack and queue. They also include dictionaries that hold key-value pairs; each entry having both a unique key and a separate value object.
  • Bit-Based Collections. The bit-based collections hold groups of binary digits. The functionality of these collections is specific to the handling of bit arrays, including setting and resetting of individual bits and performing bitwise logical operations.
  • Specialised (or Specialized). Specialised collections are provided for optimised processing of specific types of data or structure.

Each type of collection will be investigated in future articles in the C# Fundamentals tutorial. It is also possible to create your own collection types when the provided options do not meet your exact requirements. However, this is beyond the scope of a beginner's tutorial.

Collection Interfaces

All of the collection types use a set of common interfaces. The interface is an object-oriented programming concept that will be described in a future tutorial. In simple terms, interfaces describe a number of public properties and methods that must be provided by any class that implements the interface. The shared interfaces define the basic functionality of each collection class. This means that one collection type can often be easily substituted for another as their behaviours will be similar.

Each of the key interfaces is described below. Note that not all of the collections implement every interface.

ICollection Interface

The first interface is ICollection. Every collection implements this interface, its properties and methods. The properties and methods are described in the following sections with examples for the key items.

NB: All of the collection interfaces have an "I" prefix. This is a common naming convention.

ICollection.CopyTo

The CopyTo method allows you to copy each item from a collection into an array. The basic CopyTo method accepts two parameters. The first is the array to fill. The second is an integer representing the index of the first item in the array to copy to. Each item from the collection is placed in subsequent indexes of the array.

In the example below, the collection is copied to index zero of the array, which is the first element. The example demonstrates the use of CopyTo for the ArrayList, a commonly used collection that provides a one-dimensional dynamic array structure. The ArrayList is found in the System.Collections namespace so to execute the example, add using System.Collections; to the source code.

ArrayList names = new ArrayList();

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

// Copy the collection into a new array of strings
string[] namesArray = new string[3];
names.CopyTo(namesArray, 0);

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

/* OUTPUT

Andrew
Alex
Adrienne

*/

NB: Strings are used throughout this article within the examples. These are used for ease of demonstration only and could be replaced with any type of object.

24 April 2007