Array and Collection Interfaces C# Help

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 is an interface that is used by the foreach statement to iterate through the array. Because this is a very special feature, it is discussed in the next section, “Enumerations.”


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 “I collection and for synchronization.


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


The property IsSynchronized defines whether the collection is threadsafe  For arrays, this property always returns false. For synchronized access, the SyncRoot property can be used for thread-safe access. Chapter 19, “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 ( ).


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 efines 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, because these do not apply to arrays. All the properties and methods of the IList interface are shown in the following table.


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

Clear( )

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


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.


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.


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.


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


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


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

Posted on October 28, 2015 in Arrays

Share the Story

Back to Top
Share This