Array Methods
Part of the Fundamentals section of Coddy's C# journey — lesson 58 of 69.
Arrays are packed with many methods (functionalities). To access a method, write:
Array.MethodName(arrayName, otherParameters)Common Array Methods:
Clear(array, index, length) - clears the elements of an array, setting a range of elements to zero, false, or null:
int[] numbers = {1, 2, 3, 4, 5};
Array.Clear(numbers, 1, 3);
// numbers will be {1, 0, 0, 0, 5}Reverse(array) - reverses the order of the elements in an array:
int[] numbers = {1, 2, 3, 4, 5};
Array.Reverse(numbers);
// numbers will be {5, 4, 3, 2, 1}Sort(array) - sorts the elements in an array in ascending order:
int[] numbers = {5, 2, 9, 1, 5, 6};
Array.Sort(numbers);
// numbers will be {1, 2, 5, 5, 6, 9}Copy(sourceArray, sourceIndex, destinationArray, destinationIndex, length) - copies a range of elements from one array to another:
int[] source = {1, 2, 3, 4, 5};
int[] destination = new int[5];
Array.Copy(source, 0, destination, 0, 5);
// destination will be {1, 2, 3, 4, 5}Example with different positions:
int[] source = {1, 2, 3, 4, 5};
int[] destination = new int[7];
Array.Copy(source, 1, destination, 2, 3);
// destination will be {0, 0, 2, 3, 4, 0, 0}
// Copied elements 2, 3, 4 from source starting at index 1
// Placed them in destination starting at index 2Challenge
EasyCreate a method named Merge that receives two arrays as arguments. The method merges the two arrays into one sorted array and returns it.
For example the following arguments: Merge(new string[] {"1", “4”, “2”}, new string[] {"2", “5”, “9”}) will return ["1", “2”, “2”, “4”, “5”, “9”]
Use Array.Copy() to copy elements from one array to another. The syntax is:
Array.Copy(sourceArray, sourceStartPosition, destinationArray, destinationStartPosition, length)For example:
// Source array
string[] sourceArray = {"1", "2", "3", "4", "5"};
// Destination array
string[] destinationArray = new string[5];
// Copy elements from sourceArray to destinationArray
Array.Copy(sourceArray, 0, destinationArray, 0, 5);Destination array after copy: 1 2 3 4 5
Cheat sheet
Arrays have built-in methods accessed with Array.MethodName() syntax:
Clear(array, index, length)- sets elements to zero, false, or nullReverse(array)- reverses element orderSort(array)- sorts elements in ascending orderCopy(sourceArray, sourceIndex, destinationArray, destinationIndex, length)- copies elements between arrays
Examples:
int[] numbers = {1, 2, 3, 4, 5};
Array.Clear(numbers, 1, 3);
// numbers will be {1, 0, 0, 0, 5}int[] numbers = {1, 2, 3, 4, 5};
Array.Reverse(numbers);
// numbers will be {5, 4, 3, 2, 1}int[] numbers = {5, 2, 9, 1, 5, 6};
Array.Sort(numbers);
// numbers will be {1, 2, 5, 5, 6, 9}int[] source = {1, 2, 3, 4, 5};
int[] destination = new int[5];
Array.Copy(source, 0, destination, 0, 5);
// destination will be {1, 2, 3, 4, 5}Try it yourself
using System;
public class Program {
public static string[] Merge(string[] arr1, string[] arr2) {
// Write code here
}
public static void Main(string[] args) {
string textArr1 = Console.ReadLine();
string textArr2 = Console.ReadLine();
string[] arr1 = textArr1.Split(",");
string[] arr2 = textArr2.Split(",");
string[] mergedArray = Merge(arr1, arr2);
Console.WriteLine(string.Join(", ", mergedArray));
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic Shortcuts5Operators Part 2
Comparison OperatorsLogical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 311Arrays Basics
Declaring ArraysAccessing ElementsModifying ArraysArray MethodsRecap - Product ArrayEdit Recap - Reversed Array