How Array Indexing Works
๐ก Concept Name
Array Indexing โ The process of accessing array elements using their position (index) in the memory block.
๐ Quick Intro
Indexing allows you to access individual elements of an array by their position. The index starts at 0 and increments by 1 for each subsequent element.
๐ง Analogy / Short Story
Think of an array as a row of apartment units. Each unit has a number starting from 0. To visit a friend, you go directly to the unit number (index) instead of checking every door.
๐ง Technical Explanation
- ๐ Indexing starts at 0 (zero-based indexing).
- ๐ง Address of any element = base address + (index ร size of element).
- โ๏ธ Allows constant-time access: O(1).
- โ Index out of bounds throws runtime exception in C#.
- ๐พ Supports efficient memory access patterns due to contiguous storage.
array[array.Length]
instead of array[array.Length - 1]
! It's a classic "off-by-one" error, and trust me, even pros get caught by it once in a while.
๐ฏ Purpose & Use Case
- โ Fast element access using known positions.
- โ Basis for higher data structures like matrices, hash tables, and more.
- โ Efficient iteration and traversal patterns.
myArray[myArray.Length - 1]
instead of myArray[-1]
.
๐ป Real Code Example
int[] numbers = { 10, 20, 30, 40, 50 };
Console.WriteLine(numbers[0]); // Output: 10
Console.WriteLine(numbers[3]); // Output: 40
// Invalid index
// Console.WriteLine(numbers[5]); // Throws IndexOutOfRangeException

โ Interview Q&A
Q1: What is array indexing?
A: Accessing array elements using their position number.
Q2: Where does array indexing start?
A: At index 0.
Q3: What is the time complexity of accessing an array index?
A: O(1)
Q4: What happens on accessing invalid index?
A: An IndexOutOfRangeException is thrown.
Q5: Can you use a negative index?
A: No, indexes must be non-negative.
Q6: Whatโs the formula to get address of element?
A: base_address + index ร size_of_element
Q7: Is indexing in arrays sequential?
A: Yes, it follows the order of allocation.
Q8: Can indexing be customized in C# arrays?
A: No, it's fixed at 0-based.
Q9: How are multi-dimensional arrays indexed?
A: With multiple indices: arr[i, j]
Q10: Does array indexing impact performance?
A: Yes, it enables fast and predictable access.
๐ MCQs
Q1. What does array indexing allow?
- Access by name
- Accessing elements by position
- Group values
- Delete values
Q2. What is the first valid index in C# array?
- 0
- 1
- Depends on length
- -1
Q3. What’s the time complexity to access an index?
- O(1)
- O(n)
- O(log n)
- O(n^2)
Q4. What exception is thrown on invalid index?
- InvalidIndexException
- NullReferenceException
- IndexOutOfRangeException
- AccessViolationException
Q5. What’s the formula to calculate memory address?
- Base + index
- Base - index
- Base + (index × element size)
- Index × element
Q6. Can you use index -1?
- Yes
- No
- Only with arrays
- Only with Lists
Q7. What’s the last index of array of size 5?
- 5
- 4
- 0
- Depends
Q8. Which collection supports custom indexing?
- Array
- List
- Dictionary
- Stack
Q9. Can you modify value using indexing?
- No
- Yes
- Only strings
- Only integers
Q10. Are arrays zero-based in C#?
- Yes
- No
- Optional
- Depends
๐ก Bonus Insight
For read-heavy scenarios, arrays provide excellent performance due to direct indexing. However, use List<T>
when flexibility and resizing are required.
Personal Advice: If your code suddenly starts crashing, always check if your index is within 0
and array.Length - 1
. Debugging becomes so much easier!
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!