C# Array Class

The Array class plays the main role when we declare an array using brackets in C#. The brackets are a shorthand way to declare the arrays. When we create an array using brackets the array class basically creates a new class internally which derives from an abstract base class called Array.

This is why we get a number of useful properties and methods with the array we have defined. These properties and methods are defined in the class called Array.

In the previous section while traversing the array we have used the Length property which comes from the Array class.

Following is the list of some important properties and methods of the Array class.

Properties

Property Name Data Type Description
IsFixedSize bool Used to Get a value indicating whether the System.Array has a fixed size. This property is always true for all arrays.
IsReadOnly bool Used to Get a value indicating whether the System.Array is read-only. This property is always false for all arrays.
IsSynchronized bool Used to Get a value indicating whether access to the System.Array is synchronized. This property is always true for all arrays.
Length int Used to Get a 32-bit integer that represents the total number of elements in all the dimensions of the System.Array.
LongLength long Used to Get a 64-bit integer that represents the total number of elements in all the dimensions of the System.Array.
Rank int Used to Get the rank (number of dimensions) of the System.Array.
SyncRoot object Used to Get an object that can be used to synchronize access to the System.Array.

Methods

Method Name Return Type Description
AsReadOnly<T>(T[] array) ReadOnlyCollection<T> Returns a read-only wrapper for the specified array.

BinarySearch(Array array, object value)
BinarySearch<T>(T[] array, T value)
BinarySearch(Array array, object value, IComparer comparer)
BinarySearch<T>(T[] array, T value, IComparer<T> comparer)
BinarySearch(Array array, int index, int length, object value)
BinarySearch<T>(T[] array, int index, int length, T value)
BinarySearch(Array array, int index, int length, object value, IComparer comparer) BinarySearch<T>(T[] array, int index, int length, T value, IComparer<T> comparer)

int Searches an entire one-dimensional sorted System.Array for a specific element.
Clone object Used to Create a shallow copy of the System.Array.
Clear void Used to Set a range of elements in the System.Array to zero, to false, or to null, depending on the element type.
Copy void Used to copy elements of an array into another array by specifying starting index.
CopyTo(Array array, int index)
CopyTo(Array array, long index)
void It copies all the elements of the current one-dimensional array to the specified one-dimensional array starting at the specified destination array index
CreateInstance Array Creates a one-dimensional System.Array of the specified System.Type and length, with zero-based indexing.
Exists<T>(T[] array, Predicate<T> match) bool Determines whether the specified array contains elements that match the conditions defined by the specified predicate.
Find<T>(T[] array, Predicate<T> match) T Used to search for an element that matches the conditions defined by the specified predicate and returns the first occurrence within the entire System.Array.
FindIndex int Used to search for an element that matches the conditions defined by the specified predicate and returns the zero-based index of the first occurrence within the entire System.Array.
FindLast T Searches for an element that matches the conditions defined by the specified predicate and returns the zero-based index of the last occurrence within the entire System.Array.
FindLastIndex int Used to search for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the entire System.Array.
ForEach<T>(T[] array, Action<T> action) void Used to traverse the Array
GetLength int Returns the 32-bit length of the Array
GetLongLength long Return64-bit64 bit length of the Array
GetLowerBound int Gets the lower bound of the specified dimension in the System.Array.
GetUpperBound int Gets the upper bound of the specified dimension in the System.Array.
GetValue object Used to get the value from Array
SetValue void Used to set a value to the element at the specified position in the one-dimensional
Reverse void Used to reverse the array elements
Sort void Used to sort the elements in an entire one-dimensional System.Array

Let's learn about the Passing array as parameter in the next section.