0
0
C Sharp (C#)programming~10 mins

Array methods (Sort, Reverse, IndexOf) in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array methods (Sort, Reverse, IndexOf)
Start with array
Choose method: Sort, Reverse, or IndexOf
Sort: rearrange elements ascending
Reverse: flip elements order
IndexOf: find position of element
Return result or modify array
End
Start with an array, pick a method (Sort, Reverse, IndexOf), then either rearrange elements or find an element's position, and finally get the result.
Execution Sample
C Sharp (C#)
int[] numbers = {3, 1, 4, 1, 5};
Array.Sort(numbers);
Array.Reverse(numbers);
int pos = Array.IndexOf(numbers, 1);
Sorts the array ascending, reverses it, then finds the index of the first '1'.
Execution Table
StepActionArray StateIndexOf Search ElementIndexOf Result
1Initial array[3, 1, 4, 1, 5]--
2Sort array ascending[1, 1, 3, 4, 5]--
3Reverse array[5, 4, 3, 1, 1]--
4IndexOf search for 1[5, 4, 3, 1, 1]13
5End[5, 4, 3, 1, 1]--
💡 IndexOf found element '1' at position 3, execution ends.
Variable Tracker
VariableStartAfter SortAfter ReverseAfter IndexOf
numbers[3,1,4,1,5][1,1,3,4,5][5,4,3,1,1][5,4,3,1,1]
posundefinedundefinedundefined3
Key Moments - 3 Insights
Why does IndexOf return 3 instead of 1 when searching for '1'?
Because after sorting and reversing, the first '1' is at index 3 in the modified array (see execution_table step 4).
Does Array.Sort change the original array or create a new one?
Array.Sort changes the original array in place, as shown by the array state changing from step 1 to step 2.
What happens if the element searched by IndexOf is not in the array?
IndexOf returns -1 to indicate the element is not found, but in this example the element '1' is found at index 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the array state after the Reverse method?
A[3, 1, 4, 1, 5]
B[5, 4, 3, 1, 1]
C[1, 1, 3, 4, 5]
D[1, 3, 4, 5, 1]
💡 Hint
Check the 'Array State' column at step 3 in the execution_table.
At which step does the array get sorted in ascending order?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column and array changes between step 1 and 2.
If we searched for '2' using IndexOf instead of '1', what would be the result?
A-1
B3
C0
D4
💡 Hint
Recall that IndexOf returns -1 if the element is not found in the array.
Concept Snapshot
Array methods in C#:
- Array.Sort(array): sorts elements ascending in place.
- Array.Reverse(array): reverses elements order in place.
- Array.IndexOf(array, value): returns first index of value or -1 if not found.
These methods modify the original array except IndexOf which returns a position.
Full Transcript
We start with an array of numbers. First, Array.Sort rearranges the elements in ascending order, changing the array from [3,1,4,1,5] to [1,1,3,4,5]. Next, Array.Reverse flips the array order to [5,4,3,1,1]. Then, Array.IndexOf searches for the first occurrence of '1' in the reversed array and finds it at index 3. The variable 'pos' stores this index. The array methods Sort and Reverse change the original array directly, while IndexOf returns the position of the searched element or -1 if not found.