Lesson 7 of 15
Arrays
Arrays
Arrays store a fixed-size sequence of elements of the same type:
int[] numbers = { 4, 2, 7, 1, 9 };
Console.WriteLine(numbers[0]); // 4 (zero-indexed)
Console.WriteLine(numbers.Length); // 5
Sorting and Searching
int[] nums = { 3, 1, 4, 1, 5, 9 };
Array.Sort(nums);
// nums is now { 1, 1, 3, 4, 5, 9 }
Iterating
foreach (int n in numbers) {
Console.WriteLine(n);
}
Your Task
Write a static method Sum(int[] arr) that returns the sum of all elements.
WasmSharp (.NET) loading...
Loading...
Click "Run" to execute your code.