Array and Collection Interfaces


The Array class implements the interfaces IEnumerable, ICollection, and IList for accessing and enumerating the elements of the array. Because with a custom array a class is created that derives from the abstract class Array, you can use the methods and properties of the implemented interfaces with an array variable.

IEnumerable

IEnumerable is an interface that is used by the foreach statement to iterate through the array. As this is a very special feature, it will be discussed in the next section.

ICollection

The interface ICollection derives from the interface IEnumerable and has additional properties and methods as shown in the following table. This interface is mainly used to get the number of elements in a collection and for synchronization.

Open table as spreadsheet

ICollection Interface Properties and Methods

Description

Count

The Count property gives you the number of elements inside the collection. The Count property returns the same value as the Length property.

IsSynchronized SyncRoot

The property IsSynchronized defines if the collection is thread-safe. For arrays, this property always returns false. For synchronized access, the SyncRoot property can be used for thread-safe access. Both of these properties are defined with the ICollection interface. Chapter 18, “Threading and Synchronization,” explains threads and synchronization, and there you can read how to implement thread safety with collections.

CopyTo()

With the CopyTo() method you can copy the elements of an array to an existing array. This is similar to the static method Array.Copy().

IList

The IList interface derives from the interface ICollection and defines additional properties and methods. The major reason why the Array class implements the IList interface is that the IList interface defines the Item property for accessing the elements using an indexer. Many of the other IList members are implemented by the Array class by throwing a NotSupportedException, as these do not apply to arrays. All the properties and methods of the IList interface are shown in the following table.

Open table as spreadsheet

IList Interface

Description

Add()

The Add() method is used to add elements to a collection. With arrays, the method throws NotSupportedException.

Clear()

The Clear() method empties all elements of the array. Value types are set to 0, reference types to null.

Contains()

With the Contains() method, you can find out if an element is within the array. The return value is true or false. This method does a linear search through all elements of the array until the element is found.

IndexOf()

The IndexOf() method does a linear search through all elements of the array similar to the Contains() method. What’s different is that the IndexOf() method returns the index of the first element found.

Insert() Remove() RemoveAt()

With collections, the Insert() method is used to insert elements; with Remove() and RemoveAt(), elements can be removed. With arrays, all these methods throw a NotSupportedException.

IsFixedSize

As arrays are always fixed in size, so this property always returns true.

IsReadOnly

Arrays are always read/write, so this property returns false. In Chapter 10, “Collections,” you can read how to create a read-only collection from an array.

Item

The Item property allows accessing the array using an integer index.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net