12 minlesson

Array Operations

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:

csharp
1using System;
2
3int[] numbers = { 5, 2, 8, 1, 9 };
4
5// Sort the array
6Array.Sort(numbers);
7// numbers is now { 1, 2, 5, 8, 9 }

Sorting

Basic Sort

csharp
1int[] numbers = { 5, 2, 8, 1, 9 };
2Array.Sort(numbers);
3// { 1, 2, 5, 8, 9 }
4
5string[] names = { "Charlie", "Alice", "Bob" };
6Array.Sort(names);
7// { "Alice", "Bob", "Charlie" }

Reverse Sort (Descending)

csharp
1int[] 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:

csharp
1string[] names = { "Alice", "Bob", "Charlie" };
2int[] scores = { 85, 95, 75 };
3
4// Sort both arrays based on scores
5Array.Sort(scores, names);
6// scores: { 75, 85, 95 }
7// names: { "Charlie", "Alice", "Bob" }

Reversing

csharp
1int[] numbers = { 1, 2, 3, 4, 5 };
2Array.Reverse(numbers);
3// { 5, 4, 3, 2, 1 }
4
5// Reverse a portion
6int[] nums = { 1, 2, 3, 4, 5, 6 };
7Array.Reverse(nums, 2, 3); // Reverse 3 elements starting at index 2
8// { 1, 2, 5, 4, 3, 6 }

Searching

IndexOf and LastIndexOf

csharp
1string[] fruits = { "Apple", "Banana", "Cherry", "Banana", "Date" };
2
3int first = Array.IndexOf(fruits, "Banana"); // 1
4int last = Array.LastIndexOf(fruits, "Banana"); // 3
5int notFound = Array.IndexOf(fruits, "Grape"); // -1
6
7if (Array.IndexOf(fruits, "Cherry") >= 0)
8{
9 Console.WriteLine("Cherry found!");
10}

BinarySearch (for sorted arrays)

csharp
1int[] sorted = { 1, 3, 5, 7, 9, 11, 13 };
2
3int index = Array.BinarySearch(sorted, 7); // 3
4int notFound = Array.BinarySearch(sorted, 6); // Negative (not found)
5
6// BinarySearch is faster for large sorted arrays
7// But array MUST be sorted first!

Contains, Any, All (LINQ)

csharp
1using System.Linq;
2
3int[] numbers = { 1, 2, 3, 4, 5 };
4
5bool hasThree = numbers.Contains(3); // true
6bool hasNegative = numbers.Any(n => n < 0); // false
7bool allPositive = numbers.All(n => n > 0); // true
8bool anyEven = numbers.Any(n => n % 2 == 0); // true

Copying

Array.Copy

csharp
1int[] source = { 1, 2, 3, 4, 5 };
2int[] dest = new int[5];
3
4Array.Copy(source, dest, 5); // Copy all 5 elements
5// dest = { 1, 2, 3, 4, 5 }
6
7// Partial copy
8int[] partial = new int[3];
9Array.Copy(source, 1, partial, 0, 3); // Copy 3 elements from index 1
10// partial = { 2, 3, 4 }

Clone

csharp
1int[] original = { 1, 2, 3 };
2int[] clone = (int[])original.Clone();
3// clone is a separate copy

CopyTo

csharp
1int[] source = { 1, 2, 3 };
2int[] dest = new int[6];
3
4source.CopyTo(dest, 2); // Copy to dest starting at index 2
5// dest = { 0, 0, 1, 2, 3, 0 }

Filling and Clearing

Fill (C# 8+)

csharp
1int[] numbers = new int[5];
2Array.Fill(numbers, 42);
3// { 42, 42, 42, 42, 42 }
4
5// Fill a range
6int[] nums = new int[10];
7Array.Fill(nums, 99, 2, 4); // Fill 4 elements starting at index 2
8// { 0, 0, 99, 99, 99, 99, 0, 0, 0, 0 }

Clear

csharp
1int[] numbers = { 1, 2, 3, 4, 5 };
2Array.Clear(numbers, 1, 3); // Clear 3 elements from index 1
3// { 1, 0, 0, 0, 5 }
4
5// Clear all
6Array.Clear(numbers);
7// { 0, 0, 0, 0, 0 }

Resizing

Arrays have fixed size, but you can create a new array:

csharp
1int[] numbers = { 1, 2, 3 };
2
3// Resize (creates new array internally)
4Array.Resize(ref numbers, 5);
5// { 1, 2, 3, 0, 0 }
6
7Array.Resize(ref numbers, 2);
8// { 1, 2 }

Slicing (C# 8+)

Ranges

csharp
1int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
2
3int[] 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

csharp
1using System.Linq;
2
3int[] numbers = { 5, 2, 8, 1, 9, 3, 7 };
4
5// Aggregate functions
6int sum = numbers.Sum(); // 35
7double avg = numbers.Average(); // 5.0
8int min = numbers.Min(); // 1
9int max = numbers.Max(); // 9
10int count = numbers.Count(); // 7
11
12// Filtering
13int[] evens = numbers.Where(n => n % 2 == 0).ToArray(); // { 2, 8 }
14int[] large = numbers.Where(n => n > 5).ToArray(); // { 8, 9, 7 }
15
16// Transforming
17int[] doubled = numbers.Select(n => n * 2).ToArray();
18string[] strings = numbers.Select(n => n.ToString()).ToArray();
19
20// Ordering
21int[] sorted = numbers.OrderBy(n => n).ToArray();
22int[] descending = numbers.OrderByDescending(n => n).ToArray();
23
24// First and Last
25int first = numbers.First(); // 5
26int last = numbers.Last(); // 7
27int firstEven = numbers.First(n => n % 2 == 0); // 2

Practical Examples

Remove Duplicates

csharp
1int[] withDupes = { 1, 2, 2, 3, 3, 3, 4 };
2int[] unique = withDupes.Distinct().ToArray();
3// { 1, 2, 3, 4 }

Find Common Elements

csharp
1int[] 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

csharp
1int[] arr1 = { 1, 2, 3 };
2int[] arr2 = { 4, 5, 6 };
3
4// Using Concat
5int[] combined = arr1.Concat(arr2).ToArray();
6// { 1, 2, 3, 4, 5, 6 }
7
8// Using spread (C# 12+)
9// int[] combined = [..arr1, ..arr2];

Top N Elements

csharp
1int[] 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() and Array.Reverse() for ordering
  • Array.IndexOf() and Array.BinarySearch() for finding elements
  • Array.Copy() and Clone() for duplicating arrays
  • Array.Fill() and Array.Clear() for initialization
  • Range syntax [start..end] for slicing
  • LINQ methods for advanced operations (Sum, Where, OrderBy, etc.)

Next, we'll explore multidimensional arrays.