System.Array

C# implements arrays with the class System.Array. The Array class has a number of useful methods. Table 15-1 shows a few of the more important methods and properties of the System.Array class.

Table 15-1. Useful methods and properties of System.Array

Method or Property

Description

Clear()

Public static method that sets a range of elements in the array to zero or to a null reference

Copy()

Overloaded public static method that copies a section of one array to another array

IndexOf()

Overloaded public static method that returns the index (offset) of the first instance of a value in a one-dimensional array

LastIndexOf()

Overloaded public static method that returns the index of the last instance of a value in a one-dimensional array

Reverse()

Overloaded public static method that reverses the order of the elements in a one-dimensional array

Sort()

Overloaded public static method that sorts the values in a one-dimensional array

IsFixedSize

Public property that returns a value indicating whether the array has a fixed size

Length

Public property that returns the length of the array

Rank

Public property that returns the number of dimensions of the array

The Array class’s static methods, Reverse() and Sort(), make manipulation of the objects within the array very easy. Note, however, that to reverse or sort the elements of the array, they must be of a type that implements the IComparable interface. (Chapter 16 describes ...

Get Learning C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.