Array Methods and Properties

Vaibhav • September 11, 2025

By now you know how to declare and initialize arrays and the difference between rectangular (multidimensional) and jagged arrays. Arrays in C# are more than simple containers - every array you create is an instance of System.Array, which gives you useful properties and methods for inspecting and manipulating array data efficiently. In this article we’ll walk through the most important properties and methods, explain what they do, show compact examples, and highlight performance and correctness notes you should care about.

Arrays are a language feature (square-bracket syntax) and also objects of the System.Array class. That dual nature is why arrays come with built-in behaviors in addition to their syntax.

Key properties you’ll use every day

These properties are extremely cheap (constant-time) and are the first things you’ll check when working with an array.

// Length, Rank and GetLength examples
int[] numbers = { 10, 20, 30, 40, 50 };
Console.WriteLine($"Total elements: {numbers.Length}"); // 5

int\[,] matrix = new int\[3, 4];
Console.WriteLine(matrix.Rank);       // 2 (two dimensions)
Console.WriteLine(matrix.GetLength(0)); // 3 (rows)
Console.WriteLine(matrix.GetLength(1)); // 4 (columns)

Quick explanation: Length returns the total number of elements in the entire array. For multidimensional arrays, Rank gives the number of dimensions and GetLength(dimension) returns the size of a specific dimension (zero-based index).

For a 2D array declared as new int[rows,cols], Length equals rows × cols. Use GetLength() when you mean “rows” or “columns” specifically.

Essential methods (what they do and when to use them)

The System.Array class exposes a handful of commonly used methods. They are well-tested and optimized - prefer them over hand-rolled loops when appropriate.

Array.Sort()

Sorts an array in-place (ascending order by default). Works for primitive types and any types that implement IComparable or with a custom comparer.

int[] scores = { 50, 20, 90, 10, 70 };
Array.Sort(scores);
// scores is now: 10, 20, 50, 70, 90

Array.Sort() uses optimized algorithms (introspective sort variants). Average complexity is O(n log n). For large arrays, sorting can dominate runtime - measure with realistic sizes.

Array.Reverse()

Reverse the elements in-place. Often combined with Sort() to get descending order.

string[] names = { "Alice", "Bob", "Charlie" };
Array.Reverse(names); // names becomes { "Charlie", "Bob", "Alice" }

Array.IndexOf()

Finds the index of the first matching element (returns -1 if not found).

int[] values = { 5, 10, 15, 20 };
int i = Array.IndexOf(values, 15); // i == 2

Array.Copy()

Copies elements from one array to another. This is a shallow copy for reference types (it copies references).

int[] original = { 1, 2, 3, 4, 5 };
int[] copy = new int[original.Length];
Array.Copy(original, copy, original.Length);

For reference-type arrays, Array.Copy() copies object references, not the underlying objects. If you need full object duplication, you must clone or deep-copy the objects themselves.

Array.Clear()

Resets elements to their default values (0 for numeric types, null for reference types).

int[] data = { 1, 2, 3, 4, 5 };
Array.Clear(data, 1, 3); // data becomes {1,0,0,0,5}

Common pitfalls and correctness notes

  • Bounds checking: Accessing arr[index] outside valid range throws IndexOutOfRangeException. Always prefer for loops using Length or safe iteration with foreach.
  • Shallow vs deep copies: Copying arrays of references copies pointers - not the objects.
  • In-place mutations: Methods like Sort() and Reverse() modify the original array. If you must keep the original, copy first.

Performance overview

Most array properties are cheap. Method costs vary:

  • Length and Rank - O(1).
  • IndexOf - O(n) (linear scan) in worst case.
  • Copy and Clear - O(k) where k is number of elements copied/cleared.
  • Sort - O(n log n) typical; can be expensive for large n.

If you need a dynamic-sized container with frequent inserts/removes, prefer List<T>. Use arrays when size is known or fixed and when you need the most compact, contiguous memory layout for speed.

Small practical examples (concise and explained)

// 1) Sort and display
int[] arr = { 3, 1, 4, 2 };
Array.Sort(arr);
foreach (var x in arr) Console.Write(x + " "); // 1 2 3 4

// 2) Copy subset into another array
int\[] src = { 10, 20, 30, 40, 50 };
int\[] dst = new int\[3];
Array.Copy(src, 1, dst, 0, 3); // dst = {20,30,40}

// 3) Clear a range
Array.Clear(src, 2, 2); // src now {10,20,0,0,50}

When to rely on array methods vs write custom loops

Built-in array methods are optimized and less error-prone. Prefer them for standard operations (sort, copy, reverse). Write custom loops when you need specialized behavior, custom comparison rules, or streaming processing that avoids allocating intermediate arrays.

Summary

Arrays in C# are powerful: they combine simple syntax with the capabilities of System.Array. Remember the main toolbox: Length, Rank, GetLength(), and the methods Sort(), Reverse(), IndexOf(), Copy(), and Clear(). Each is optimized for common scenarios - use them to keep your code concise and correct.

In the next articles we’ll explore generic collections (List<T>, Dictionary<K,V>) that offer more flexibility and higher-level operations when arrays are too rigid.