Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Array indexing and access in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Array indexing and access
Create array with elements
↓
Access element at index i
↓
Retrieve value stored at index i
↓
Use value (print, assign, etc.)
↓
End or repeat for other indices
This flow shows how an array is created, then an element is accessed by its index to retrieve and use its value.
Execution Sample
C Sharp (C#)
int[] numbers = {10, 20, 30, 40};
int value = numbers[2];
Console.WriteLine(value);
This code creates an array of integers, accesses the element at index 2, and prints it.
Execution Table
StepActionIndex AccessedValue RetrievedOutput
1Create array 'numbers'---
2Access element at index 2230-
3Assign value to variable 'value'-30-
4Print 'value'-3030
5End---
💡 Program ends after printing the value at index 2.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
numbersnull{10, 20, 30, 40}{10, 20, 30, 40}{10, 20, 30, 40}
valueundefinedundefined3030
Key Moments - 3 Insights
Why does accessing numbers[2] give 30 and not 20 or 40?
Array indexing starts at 0, so numbers[0] is 10, numbers[1] is 20, and numbers[2] is 30 as shown in step 2 of the execution table.
What happens if we try to access an index outside the array, like numbers[4]?
Accessing an index outside the array bounds causes a runtime error (IndexOutOfRangeException), because the array only has indices 0 to 3.
Is the value copied or referenced when we assign numbers[2] to 'value'?
For value types like int, the value is copied. So 'value' holds its own copy of 30, independent of the array.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'value' after step 3?
A30
B20
Cundefined
D40
💡 Hint
Check the 'Value Retrieved' column at step 3 in the execution table.
At which step is the array 'numbers' created?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column in the execution table for array creation.
If we change the index accessed from 2 to 1, what value would be retrieved?
A10
B20
C30
D40
💡 Hint
Refer to the array contents in the variable tracker and the indexing rule explained in key moments.
Concept Snapshot
Array indexing starts at 0.
Access elements using array[index].
Index must be within 0 to length-1.
Access returns the value stored at that position.
Out-of-range index causes error.
Value types are copied on access.
Full Transcript
This lesson shows how arrays in C# store multiple values and how to access them using an index starting at zero. We create an array 'numbers' with four integers. Accessing numbers[2] retrieves the third element, which is 30. This value is assigned to a variable 'value' and printed. The execution table tracks each step, showing the array creation, index accessed, value retrieved, and output. Key points include understanding zero-based indexing, the importance of staying within array bounds, and that value types like int are copied when accessed. The visual quiz tests understanding of these steps and indexing rules.

Practice

(1/5)
1. What is the index of the first element in a C# array int[] numbers = {10, 20, 30};?
easy
A. 0
B. 1
C. 10
D. Array length - 1

Solution

  1. 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.
  2. Step 2: Identify the first element's index

    The first element 10 is at index 0, not 1 or any other number.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    First element index = 0 [OK]
Hint: Remember: C# arrays start at zero index [OK]
Common Mistakes:
  • Thinking the first index is 1
  • Confusing element value with index
  • Using array length as first index
2. Which of the following is the correct way to access the third element of an array string[] fruits = {"apple", "banana", "cherry"};?
easy
A. fruits[2]
B. fruits[3]
C. fruits(2)
D. fruits{2}

Solution

  1. Step 1: Recall C# array indexing syntax

    Arrays use square brackets [] to access elements by zero-based index.
  2. Step 2: Identify the correct index for the third element

    The third element is at index 2, so fruits[2] is correct.
  3. Final Answer:

    fruits[2] -> Option A
  4. Quick Check:

    Third element index = 2 [OK]
Hint: Use square brackets and zero-based index [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Using curly braces instead of brackets
  • Using index 3 for third element
3. What will be the output of this code?
int[] values = {5, 10, 15};
Console.WriteLine(values[1]);
medium
A. 5
B. Runtime error
C. 10
D. 15

Solution

  1. Step 1: Identify the element at index 1

    The array values has elements 5 at index 0, 10 at index 1, and 15 at index 2.
  2. Step 2: Determine the output of Console.WriteLine(values[1])

    Accessing values[1] prints 10 to the console.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    values[1] = 10 [OK]
Hint: Index 1 means second element in array [OK]
Common Mistakes:
  • Printing values[0] instead of values[1]
  • Confusing index with element value
  • Expecting index to start at 1
4. What is wrong with this code?
int[] arr = {1, 2, 3};
Console.WriteLine(arr[3]);
medium
A. No error, prints 3
B. Index out of range runtime error
C. Syntax error due to missing semicolon
D. Prints 0 by default

Solution

  1. 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.
  2. Step 2: Understand what happens when accessing invalid index

    Accessing arr[3] causes a runtime error: IndexOutOfRangeException.
  3. Final Answer:

    Index out of range runtime error -> Option B
  4. Quick Check:

    Accessing invalid index causes runtime error [OK]
Hint: Index must be less than array length [OK]
Common Mistakes:
  • Assuming index 3 is valid for 3-element array
  • Expecting default zero value for invalid index
  • Confusing syntax error with runtime error
5. Given int[] data = {2, 4, 6, 8, 10};, which expression correctly changes the last element to 20?
hard
A. data[5] = 20;
B. data[data.Length] = 20;
C. data[-1] = 20;
D. data[4] = 20;

Solution

  1. 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.
  2. Step 2: Choose the correct syntax to assign new value

    Assigning data[4] = 20; changes the last element to 20. Using data.Length or negative indexes is invalid.
  3. Final Answer:

    data[4] = 20; -> Option D
  4. Quick Check:

    Last index = Length - 1 = 4 [OK]
Hint: Last index = Length - 1 for assignment [OK]
Common Mistakes:
  • Using data.Length as index (out of range)
  • Using negative index (not supported)
  • Using index equal to array size
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard