Consider the following C# code snippet. What will be printed to the console?
int[] numbers = {10, 20, 30, 40, 50}; Console.WriteLine(numbers[2]);
Remember, array indexing in C# starts at 0.
The array numbers has elements indexed from 0 to 4. The element at index 2 is the third element, which is 30.
What will happen when this code runs?
int[] values = {1, 2, 3}; Console.WriteLine(values[3]);
Check what happens if you try to access an index outside the array bounds.
Arrays in C# have fixed size. Accessing index 3 in an array of size 3 (indices 0,1,2) causes an IndexOutOfRangeException.
Given the code below, what will be printed?
int[,] matrix = { {1, 2}, {3, 4} }; Console.WriteLine(matrix[1, 0]);
Remember the first index is the row, the second is the column.
The element at row 1, column 0 is 3.
What will this code print?
int[][] jagged = new int[][] { new int[] {1, 2}, new int[] {3, 4, 5} }; Console.WriteLine(jagged[1][2]);
Jagged arrays are arrays of arrays. Access the second array, then its third element.
The second array is {3,4,5}. The element at index 2 is 5.
What will happen when this code runs?
int[] arr = {100, 200, 300}; Console.WriteLine(arr[-1]);
Check if C# supports negative indexes for arrays.
C# arrays do not support negative indexing. Accessing arr[-1] throws IndexOutOfRangeException.