0
0
C Sharp (C#)programming~5 mins

Array bounds checking behavior in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Array bounds checking helps prevent errors by making sure you don't access parts of an array that don't exist.

When you want to safely access elements in an array without crashing your program.
When you need to avoid bugs caused by using wrong index numbers.
When you want to understand why your program throws an error when accessing invalid array positions.
Syntax
C Sharp (C#)
class Program
{
    static void Main()
    {
        int[] numbers = new int[3];
        // Accessing elements within bounds
        int first = numbers[0];

        // Accessing elements outside bounds throws an exception
        int invalid = numbers[3]; // This causes an error
    }
}

C# automatically checks if the index is within the array size.

If you try to access an index less than 0 or greater than or equal to the array length, it throws an IndexOutOfRangeException.

Examples
Trying to access any element in an empty array causes an error because there are no valid indexes.
C Sharp (C#)
int[] emptyArray = new int[0];
// Accessing any element will throw an exception
int value = emptyArray[0];
Accessing the only valid index 0 works, but index 1 is out of bounds and causes an error.
C Sharp (C#)
int[] singleElementArray = { 42 };
int first = singleElementArray[0]; // Works fine
int invalid = singleElementArray[1]; // Throws exception
The last element is at index 2. Trying to access index 3 is out of bounds.
C Sharp (C#)
int[] numbers = { 10, 20, 30 };
int last = numbers[2]; // Last valid element
int invalid = numbers[3]; // Throws exception
Sample Program

This program prints all valid elements of the array. Then it tries to access an invalid index, which causes an exception. The exception is caught and its message is printed.

C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        int[] scores = { 85, 90, 78 };

        Console.WriteLine("Scores array elements:");
        for (int index = 0; index < scores.Length; index++)
        {
            Console.WriteLine($"Index {index}: {scores[index]}");
        }

        try
        {
            Console.WriteLine("Trying to access index 3 (out of bounds):");
            int invalidScore = scores[3];
        }
        catch (IndexOutOfRangeException e)
        {
            Console.WriteLine("Error: " + e.Message);
        }
    }
}
OutputSuccess
Important Notes

Array bounds checking happens automatically in C# for safety.

Time complexity for accessing an element is O(1), but checking adds a tiny overhead.

Common mistake: forgetting that arrays start at index 0, so the last valid index is length - 1.

Use bounds checking to avoid crashes, but if you need faster code and are sure about indexes, unsafe code can skip checks (advanced topic).

Summary

Array bounds checking prevents accessing invalid indexes.

Accessing outside the array throws an IndexOutOfRangeException.

Always use indexes between 0 and array length - 1.