Codectionary / Developer documentation / C#

Array Class & Utility Methods

The static Array class, from the System namespace, provides utility methods for working with arrays - sorting, searching, resizing, and copying. Since arrays don't have many built-in instance methods of their own (unlike List<T>), this utility class fills that gap for common array tasks like Array.Sort(), Array.BinarySearch(), and Array.Resize().

Read the syntax

Array.Sort and Array.Reverse mutate an existing array. Array.BinarySearch requires an appropriately sorted array and returns either an index or a negative value when missing.

Array.Resize replaces the variable with an array of a new size, copying available elements. Clone and Array.Copy create separate arrays, though reference-type elements are still shared references.

Syntax

Array.MethodName(array);

Examples

Sorting and Reversing

Sorting an array in place and reversing its order.

int[] numbers = { 5, 2, 8, 1, 9 };

Array.Sort(numbers);
Console.WriteLine(string.Join(", ", numbers));  // 1, 2, 5, 8, 9

Array.Reverse(numbers);
Console.WriteLine(string.Join(", ", numbers));  // 9, 8, 5, 2, 1
  1. Sorting an array in place and reversing its order.
  2. Array.Sort and Array.Reverse mutate an existing array. Array.BinarySearch requires an appropriately sorted array and returns either an index or a negative value when missing.
  3. Sort prints the five values ascending; Reverse then prints the same array descending. No new sorted array is created by these calls.

Searching with BinarySearch

Binary search on a sorted array for fast lookups.

int[] sorted = { 1, 3, 5, 7, 9 };
int index = Array.BinarySearch(sorted, 7);
Console.WriteLine(index);  // 3

int missing = Array.BinarySearch(sorted, 4);
Console.WriteLine(missing);  // negative - not found
  1. Binary search on a sorted array for fast lookups.
  2. Array.Sort and Array.Reverse mutate an existing array. Array.BinarySearch requires an appropriately sorted array and returns either an index or a negative value when missing.
  3. The sorted array contains 7 at index 3. Searching for 4 returns a negative result; do not interpret that negative value as a valid index.

Resizing an Array

Array.Resize() creates a new, larger (or smaller) array with the same contents copied over.

int[] numbers = { 1, 2, 3 };
Array.Resize(ref numbers, 5);

Console.WriteLine(numbers.Length);  // 5
Console.WriteLine(string.Join(", ", numbers));  // 1, 2, 3, 0, 0
  1. Array.Resize() creates a new, larger (or smaller) array with the same contents copied over.
  2. Array.Sort and Array.Reverse mutate an existing array. Array.BinarySearch requires an appropriately sorted array and returns either an index or a negative value when missing.
  3. Resize expands the array to five elements and fills the new int slots with zero. Length prints 5 and Join shows 1, 2, 3, 0, 0.

Copying Arrays

Creating independent copies, whole or partial, of an array.

int[] original = { 1, 2, 3, 4, 5 };

int[] fullCopy = (int[])original.Clone();

int[] partial = new int[3];
Array.Copy(original, 1, partial, 0, 3);  // copy 3 elements starting at index 1
Console.WriteLine(string.Join(", ", partial));  // 2, 3, 4
  1. Creating independent copies, whole or partial, of an array.
  2. Array.Sort and Array.Reverse mutate an existing array. Array.BinarySearch requires an appropriately sorted array and returns either an index or a negative value when missing.
  3. Clone makes a full independent int array. Array.Copy takes three values starting at original index 1, so partial prints 2, 3, 4.

Best practices

  • Use string.Join() to print array contents readably - printing an array directly with WriteLine() shows its type name, not its contents
  • Always sort with Array.Sort() before calling Array.BinarySearch() - binary search requires the array to already be sorted to work correctly
  • Remember Array.Resize() actually creates a brand new array under the hood and copies the data - it is not a cheap operation for large arrays used repeatedly
  • Use a List<T> instead of repeatedly resizing an array when the size genuinely needs to change often

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading