Array Operations
C# provides many built-in methods for working with arrays. This lesson covers sorting, searching, copying, and other common operations.
The Array Class
The System.Array class provides static methods for array manipulation:
csharp1using System;23int[] numbers = { 5, 2, 8, 1, 9 };45// Sort the array6Array.Sort(numbers);7// numbers is now { 1, 2, 5, 8, 9 }
Sorting
Basic Sort
csharp1int[] numbers = { 5, 2, 8, 1, 9 };2Array.Sort(numbers);3// { 1, 2, 5, 8, 9 }45string[] names = { "Charlie", "Alice", "Bob" };6Array.Sort(names);7// { "Alice", "Bob", "Charlie" }
Reverse Sort (Descending)
csharp1int[] numbers = { 5, 2, 8, 1, 9 };2Array.Sort(numbers);3Array.Reverse(numbers);4// { 9, 8, 5, 2, 1 }
Sort with Another Array
Sort one array based on another:
csharp1string[] names = { "Alice", "Bob", "Charlie" };2int[] scores = { 85, 95, 75 };34// Sort both arrays based on scores5Array.Sort(scores, names);6// scores: { 75, 85, 95 }7// names: { "Charlie", "Alice", "Bob" }
Reversing
csharp1int[] numbers = { 1, 2, 3, 4, 5 };2Array.Reverse(numbers);3// { 5, 4, 3, 2, 1 }45// Reverse a portion6int[] nums = { 1, 2, 3, 4, 5, 6 };7Array.Reverse(nums, 2, 3); // Reverse 3 elements starting at index 28// { 1, 2, 5, 4, 3, 6 }
Searching
IndexOf and LastIndexOf
csharp1string[] fruits = { "Apple", "Banana", "Cherry", "Banana", "Date" };23int first = Array.IndexOf(fruits, "Banana"); // 14int last = Array.LastIndexOf(fruits, "Banana"); // 35int notFound = Array.IndexOf(fruits, "Grape"); // -167if (Array.IndexOf(fruits, "Cherry") >= 0)8{9 Console.WriteLine("Cherry found!");10}
BinarySearch (for sorted arrays)
csharp1int[] sorted = { 1, 3, 5, 7, 9, 11, 13 };23int index = Array.BinarySearch(sorted, 7); // 34int notFound = Array.BinarySearch(sorted, 6); // Negative (not found)56// BinarySearch is faster for large sorted arrays7// But array MUST be sorted first!
Contains, Any, All (LINQ)
csharp1using System.Linq;23int[] numbers = { 1, 2, 3, 4, 5 };45bool hasThree = numbers.Contains(3); // true6bool hasNegative = numbers.Any(n => n < 0); // false7bool allPositive = numbers.All(n => n > 0); // true8bool anyEven = numbers.Any(n => n % 2 == 0); // true
Copying
Array.Copy
csharp1int[] source = { 1, 2, 3, 4, 5 };2int[] dest = new int[5];34Array.Copy(source, dest, 5); // Copy all 5 elements5// dest = { 1, 2, 3, 4, 5 }67// Partial copy8int[] partial = new int[3];9Array.Copy(source, 1, partial, 0, 3); // Copy 3 elements from index 110// partial = { 2, 3, 4 }
Clone
csharp1int[] original = { 1, 2, 3 };2int[] clone = (int[])original.Clone();3// clone is a separate copy
CopyTo
csharp1int[] source = { 1, 2, 3 };2int[] dest = new int[6];34source.CopyTo(dest, 2); // Copy to dest starting at index 25// dest = { 0, 0, 1, 2, 3, 0 }
Filling and Clearing
Fill (C# 8+)
csharp1int[] numbers = new int[5];2Array.Fill(numbers, 42);3// { 42, 42, 42, 42, 42 }45// Fill a range6int[] nums = new int[10];7Array.Fill(nums, 99, 2, 4); // Fill 4 elements starting at index 28// { 0, 0, 99, 99, 99, 99, 0, 0, 0, 0 }
Clear
csharp1int[] numbers = { 1, 2, 3, 4, 5 };2Array.Clear(numbers, 1, 3); // Clear 3 elements from index 13// { 1, 0, 0, 0, 5 }45// Clear all6Array.Clear(numbers);7// { 0, 0, 0, 0, 0 }
Resizing
Arrays have fixed size, but you can create a new array:
csharp1int[] numbers = { 1, 2, 3 };23// Resize (creates new array internally)4Array.Resize(ref numbers, 5);5// { 1, 2, 3, 0, 0 }67Array.Resize(ref numbers, 2);8// { 1, 2 }
Slicing (C# 8+)
Ranges
csharp1int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };23int[] first3 = numbers[..3]; // { 0, 1, 2 }4int[] last3 = numbers[^3..]; // { 7, 8, 9 }5int[] middle = numbers[3..7]; // { 3, 4, 5, 6 }6int[] copy = numbers[..]; // Full copy
LINQ Operations
csharp1using System.Linq;23int[] numbers = { 5, 2, 8, 1, 9, 3, 7 };45// Aggregate functions6int sum = numbers.Sum(); // 357double avg = numbers.Average(); // 5.08int min = numbers.Min(); // 19int max = numbers.Max(); // 910int count = numbers.Count(); // 71112// Filtering13int[] evens = numbers.Where(n => n % 2 == 0).ToArray(); // { 2, 8 }14int[] large = numbers.Where(n => n > 5).ToArray(); // { 8, 9, 7 }1516// Transforming17int[] doubled = numbers.Select(n => n * 2).ToArray();18string[] strings = numbers.Select(n => n.ToString()).ToArray();1920// Ordering21int[] sorted = numbers.OrderBy(n => n).ToArray();22int[] descending = numbers.OrderByDescending(n => n).ToArray();2324// First and Last25int first = numbers.First(); // 526int last = numbers.Last(); // 727int firstEven = numbers.First(n => n % 2 == 0); // 2
Practical Examples
Remove Duplicates
csharp1int[] withDupes = { 1, 2, 2, 3, 3, 3, 4 };2int[] unique = withDupes.Distinct().ToArray();3// { 1, 2, 3, 4 }
Find Common Elements
csharp1int[] arr1 = { 1, 2, 3, 4, 5 };2int[] arr2 = { 3, 4, 5, 6, 7 };3int[] common = arr1.Intersect(arr2).ToArray();4// { 3, 4, 5 }
Combine Arrays
csharp1int[] arr1 = { 1, 2, 3 };2int[] arr2 = { 4, 5, 6 };34// Using Concat5int[] combined = arr1.Concat(arr2).ToArray();6// { 1, 2, 3, 4, 5, 6 }78// Using spread (C# 12+)9// int[] combined = [..arr1, ..arr2];
Top N Elements
csharp1int[] scores = { 85, 92, 78, 95, 88, 76, 91 };2int[] top3 = scores.OrderByDescending(s => s).Take(3).ToArray();3// { 95, 92, 91 }
Summary
In this lesson, you learned:
Array.Sort()andArray.Reverse()for orderingArray.IndexOf()andArray.BinarySearch()for finding elementsArray.Copy()andClone()for duplicating arraysArray.Fill()andArray.Clear()for initialization- Range syntax
[start..end]for slicing - LINQ methods for advanced operations (Sum, Where, OrderBy, etc.)
Next, we'll explore multidimensional arrays.