C# Array Methods

About This Index
This page is the C# Array Methods index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs.
Learn what an array is on /c-sharp/array. Use this page when you need a specific API such as Sort(), Find(), or IndexOf().
A C# array cannot grow after it is created. Prefer List<T> when you need Add and Remove. Array.Resize allocates a new array behind a ref.
How to Call an Array Method
Static helpers use Array.; instance members use a dot on the array variable:
int[] nums = { 4, 8, 15, 16, 23 };
Array.Sort(nums); // static
int ix = Array.IndexOf(nums, 15); // static
int first = (int)nums.GetValue(0); // instance
object copy = nums.Clone(); // instance C# Array Methods Table
All method guides in this C# tutorial series (32 tutorials), grouped by purpose.
Creation & wrapping
4 methodsCreate arrays at runtime, share an empty array, clone, or wrap as read-only.
| Method | Description | Tutorial |
|---|---|---|
CreateInstance() | Create a new array at runtime by element type and lengths. | Try it |
Empty() | Return a shared empty array of type T without allocating each time. | Try it |
Clone() | Make a shallow copy of the array as an object. | Try it |
AsReadOnly() | Wrap a T[] as a read-only collection without copying elements. | Try it |
Copying
3 methodsCopy elements between arrays. ConstrainedCopy rolls back on failure.
Searching
10 methodsFind values by equality or by a predicate (a small test function).
| Method | Description | Tutorial |
|---|---|---|
BinarySearch() | Fast search for a value in a sorted array; returns the index or a negative bitwise complement. | Try it |
IndexOf() | Return the first index of a value, or -1 if it is missing. | Try it |
LastIndexOf() | Return the last index of a value, or -1 if it is missing. | Try it |
Find() | Return the first element that matches a predicate, or default if none match. | Try it |
FindAll() | Return a new array of every element that matches a predicate. | Try it |
FindIndex() | Return the index of the first element that matches a predicate, or -1. | Try it |
FindLast() | Return the last element that matches a predicate, or default if none. | Try it |
FindLastIndex() | Return the index of the last predicate match, or -1. | Try it |
Exists() | Return true if any element matches a predicate. | Try it |
TrueForAll() | Return true only if every element matches a predicate. | Try it |
Sorting & rearranging
6 methodsSort, reverse, resize, clear a range, fill values, or re-run field initializers.
| Method | Description | Tutorial |
|---|---|---|
Sort() | Sort the elements of an array in place (ascending by default). | Try it |
Reverse() | Reverse the order of elements in an array (or a range) in place. | Try it |
Resize() | Replace an array with a new one of a different length (ref parameter). | Try it |
Clear() | Set a range of elements to their default values (0 / null / false). | Try it |
Fill() | Set every element (or a range) to the same value. | Try it |
Initialize() | Re-run field initializers on value-type elements in the array. | Try it |
Element access & bounds
6 methodsRead or write by index, and ask about length and lower/upper bounds (including multi-dimensional arrays).
| Method | Description | Tutorial |
|---|---|---|
GetValue() | Read an element by index as object (works for any Array rank). | Try it |
SetValue() | Write an element by index through the Array API. | Try it |
GetLength() | Return the number of elements in a given dimension (int). | Try it |
GetLongLength() | Return the length of a dimension as a long. | Try it |
GetLowerBound() | Return the starting index of a dimension (usually 0). | Try it |
GetUpperBound() | Return the last valid index of a dimension. | Try it |
Iteration & conversion
3 methodsWalk every element, get an enumerator, or map each element to a new type.
| Method | Description | Tutorial |
|---|---|---|
ForEach() | Run an action once for every element in a T[] array. | Try it |
GetEnumerator() | Return an enumerator used by foreach to walk elements. | Try it |
ConvertAll() | Create a new array by converting each element with a converter function. | Try it |
Frequently Asked Questions
Start with Sort()
Order an array in place, then practice IndexOf and BinarySearch.
12 people found this page helpful
