Arrays store many items in order. Indexing helps you find and use each item quickly.
Array indexing and access in C Sharp (C#)
class Program { static void Main() { int[] numbers = {10, 20, 30, 40}; int firstNumber = numbers[0]; // Access first item numbers[2] = 35; // Change third item } }
Array indexes start at 0, so the first item is at index 0.
Accessing an index outside the array size causes an error.
int[] emptyArray = new int[0]; // emptyArray has no items // Accessing any index will cause an error
int[] singleItemArray = { 100 }; int onlyItem = singleItemArray[0]; // 100 // singleItemArray[1] would cause an error
string[] fruits = { "apple", "banana", "cherry" };
string firstFruit = fruits[0]; // "apple"
string lastFruit = fruits[2]; // "cherry"This program shows how to access and change items in an array by their index. It prints the colors before and after changing one.
using System; class Program { static void Main() { string[] colors = { "red", "green", "blue" }; Console.WriteLine("Colors before change:"); foreach (string color in colors) { Console.WriteLine(color); } // Access and print the first color Console.WriteLine($"First color: {colors[0]}"); // Change the second color colors[1] = "yellow"; Console.WriteLine("Colors after change:"); foreach (string color in colors) { Console.WriteLine(color); } } }
Accessing an array element by index is very fast, O(1) time complexity.
Arrays use a fixed amount of memory based on their size (space complexity O(n)).
Common mistake: Trying to access an index outside the array size causes a runtime error (IndexOutOfRangeException).
Use array indexing when you know the position of the item you want. Use loops to access all items.
Arrays store items in order and use zero-based indexes to find them.
You can read or change an item by using its index inside square brackets.
Always make sure the index is inside the array size to avoid errors.
Practice
int[] numbers = {10, 20, 30};?Solution
Step 1: Understand zero-based indexing in C# arrays
Arrays in C# start counting indexes from 0, so the first element is at index 0.Step 2: Identify the first element's index
The first element 10 is at index 0, not 1 or any other number.Final Answer:
0 -> Option AQuick Check:
First element index = 0 [OK]
- Thinking the first index is 1
- Confusing element value with index
- Using array length as first index
string[] fruits = {"apple", "banana", "cherry"};?Solution
Step 1: Recall C# array indexing syntax
Arrays use square brackets [] to access elements by zero-based index.Step 2: Identify the correct index for the third element
The third element is at index 2, sofruits[2]is correct.Final Answer:
fruits[2] -> Option AQuick Check:
Third element index = 2 [OK]
- Using parentheses instead of brackets
- Using curly braces instead of brackets
- Using index 3 for third element
int[] values = {5, 10, 15};
Console.WriteLine(values[1]);Solution
Step 1: Identify the element at index 1
The arrayvalueshas elements 5 at index 0, 10 at index 1, and 15 at index 2.Step 2: Determine the output of Console.WriteLine(values[1])
Accessingvalues[1]prints 10 to the console.Final Answer:
10 -> Option CQuick Check:
values[1] = 10 [OK]
- Printing values[0] instead of values[1]
- Confusing index with element value
- Expecting index to start at 1
int[] arr = {1, 2, 3};
Console.WriteLine(arr[3]);Solution
Step 1: Check array size and valid indexes
The array has 3 elements with indexes 0, 1, and 2. Index 3 is outside the array bounds.Step 2: Understand what happens when accessing invalid index
Accessingarr[3]causes a runtime error: IndexOutOfRangeException.Final Answer:
Index out of range runtime error -> Option BQuick Check:
Accessing invalid index causes runtime error [OK]
- Assuming index 3 is valid for 3-element array
- Expecting default zero value for invalid index
- Confusing syntax error with runtime error
int[] data = {2, 4, 6, 8, 10};, which expression correctly changes the last element to 20?Solution
Step 1: Identify the last index of the array
The array has 5 elements, so indexes go from 0 to 4. The last element is at index 4.Step 2: Choose the correct syntax to assign new value
Assigningdata[4] = 20;changes the last element to 20. Usingdata.Lengthor negative indexes is invalid.Final Answer:
data[4] = 20; -> Option DQuick Check:
Last index = Length - 1 = 4 [OK]
- Using data.Length as index (out of range)
- Using negative index (not supported)
- Using index equal to array size
Ready to prove it?
Pass the test and this pattern moves unknown
