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

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

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

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 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 ( ).

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

Add()

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.

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()

With collections, the Insert () method is used to insert elements; with

Remove()

Remove() and RemoveAt( ), elements can be removed. With arrays, all these

RemoveAt()

methods throw a NotSupportedException.

IsFixedSize

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

IsReadOnly

Arrays are always read/write, so this property returns false.  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.

Posted on October 28, 2015 in Arrays

Share the Story

Back to Top