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
| Signature | Description |
|---|---|
| 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
| Method | Signature | Returns | Description |
|---|---|---|---|
| Copy | Copy(Array, Array, int) | void | Copies a range of elements from one array to another. |
| Sort | Sort(Array) | void | Sorts the elements in an entire one-dimensional array using the IComparable implementation of each element. |
| IndexOf | IndexOf(Array, Object) | int | Searches for the specified object and returns the index of its first occurrence. |
| Reverse | Reverse(Array) | void | Reverses the order of the elements in the entire one-dimensional array. |
| FindAll | FindAll(Array, Predicate<Object>) | Array | Returns an array containing all the elements that match the conditions defined by the specified predicate. |
Properties
| Property | Type | Description |
|---|---|---|
| Length | int | Gets the total number of elements in all dimensions of the Array. |
| LongLength | long | Gets a 64-bit integer that represents the total number of elements in all dimensions of the Array. |
| Rank | int | Gets 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)}");