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.
Personal Tip: I still remember my early C# daysโ€”accidentally trying to access 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.
Beginner Mistake: In C#, negative indices donโ€™t wrap from the end. If you want the last element, use 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
Real-World Example: While building a leaderboard for a coding contest, arrays let me jump directly to any participantโ€™s score by index. No searching, just quick and easy access!

โ“ 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!

Your Turn: Ever had a wild โ€œoff-by-oneโ€ bug? Whatโ€™s your best array tip? Share below!

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: