Microsoft

System.Array

Overview

The Array class provides methods for creating, manipulating, searching, and sorting arrays, as well as for retrieving the Length and lower bound of each dimension.

int[] numbers = new int[5];
Array.Sort(numbers);
int length = numbers.Length;

Constructors

SignatureDescription
Array.CreateInstance(Type, int)Creates a one-dimensional array of the specified type and length.
Array.CreateInstance(Type, int[])Creates a multidimensional array of the specified type and dimensions.

Methods

MethodSignatureReturnsDescription
CopyCopy(Array, Array, int)voidCopies a range of elements from one array to another.
SortSort(Array)voidSorts the elements in an entire one-dimensional array using the IComparable implementation of each element.
IndexOfIndexOf(Array, Object)intSearches for the specified object and returns the index of its first occurrence.
ReverseReverse(Array)voidReverses the order of the elements in the entire one-dimensional array.
FindAllFindAll(Array, Predicate<Object>)ArrayReturns an array containing all the elements that match the conditions defined by the specified predicate.

Properties

PropertyTypeDescription
LengthintGets the total number of elements in all dimensions of the Array.
LongLengthlongGets a 64-bit integer that represents the total number of elements in all dimensions of the Array.
RankintGets the number of dimensions of the Array.

Examples

// Create a two‑dimensional array of integers
int[,] matrix = new int[3, 3];
int counter = 1;
for (int i = 0; i < matrix.GetLength(0); i++)
{
    for (int j = 0; j < matrix.GetLength(1); j++)
    {
        matrix[i, j] = counter++;
    }
}
Console.WriteLine($"Rows: {matrix.GetLength(0)}, Columns: {matrix.GetLength(1)}");