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, 1Searching 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 foundResizing 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, 0Copying 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, 4Best 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
At a glance
- Purpose
- Applications on the .NET platform
- File extension
- .cs
- Runs in
- .NET runtime
- Usually used with
- .NET SDK and libraries
Specifications & further reading
Related C# documentation
List<T>
List<T> is a resizable, generic collection from System.Collections.Generic, and the most commonly used collection type in C#. Unlike a plain array, a List<T> automatically grows as elements are added, and it provides a rich set of methods for adding, removing, searching, and sorting. The <T> means a List can be strongly typed to hold any specific type, like List<string> or List<int>.Dictionary<TKey, TValue>
Dictionary<TKey, TValue> stores data as key-value pairs, offering fast average-case lookup, insertion, and deletion by key, backed by a hash table. Keys must be unique - adding a value with an existing key throws an exception, while indexer assignment (dict[key] = value) overwrites it instead. Dictionary does not guarantee any particular iteration order.Queue<T> & Stack<T>
Queue<T> is a first-in-first-out (FIFO) collection - items are added with Enqueue() and removed with Dequeue(), just like a real-world line. Stack<T> is last-in-first-out (LIFO) - items are added with Push() and removed with Pop(), like a stack of plates. Both are useful for specific processing orders where a general-purpose List<T> would require extra bookkeeping.HashSet<T>
HashSet<T> is a collection that stores unique elements with no guaranteed ordering, backed by a hash table. Adding a duplicate element has no effect, since HashSet automatically enforces uniqueness. It provides very fast average-case performance for adding, removing, and checking membership, and offers built-in set operations like union, intersection, and difference.
List<T> is a resizable, generic collection from System.Collections.Generic, and the most commonly used collection type in C#. Unlike a plain array, a List<T> automatically grows as elements are added, and it provides a rich set of methods for adding, removing, searching, and sorting. The <T> means a List can be strongly typed to hold any specific type, like List<string> or List<int>.Dictionary<TKey, TValue>
Dictionary<TKey, TValue> stores data as key-value pairs, offering fast average-case lookup, insertion, and deletion by key, backed by a hash table. Keys must be unique - adding a value with an existing key throws an exception, while indexer assignment (dict[key] = value) overwrites it instead. Dictionary does not guarantee any particular iteration order.Queue<T> & Stack<T>
Queue<T> is a first-in-first-out (FIFO) collection - items are added with Enqueue() and removed with Dequeue(), just like a real-world line. Stack<T> is last-in-first-out (LIFO) - items are added with Push() and removed with Pop(), like a stack of plates. Both are useful for specific processing orders where a general-purpose List<T> would require extra bookkeeping.HashSet<T>
HashSet<T> is a collection that stores unique elements with no guaranteed ordering, backed by a hash table. Adding a duplicate element has no effect, since HashSet automatically enforces uniqueness. It provides very fast average-case performance for adding, removing, and checking membership, and offers built-in set operations like union, intersection, and difference.