DEV Community

Sabin Sim
Sabin Sim

Posted on

27. C# (Arrays)

Full Runnable Code

using System;

class Program
{
    static void Main()
    {
        // 1) Declare + allocate (check default values)
        int[] numbers = new int[3];

        Console.WriteLine("Default values:");
        Console.WriteLine(numbers[0]);
        Console.WriteLine(numbers[1]);
        Console.WriteLine(numbers[2]);
        Console.WriteLine();

        // 2) Update values
        numbers[0] = 10;
        numbers[1] = 20;
        numbers[2] = 30;

        Console.WriteLine("After updates:");
        for (int i = 0; i < numbers.Length; i++)
        {
            Console.WriteLine(numbers[i]);
        }
        Console.WriteLine();

        // 3) Index from End (^)
        int[] numbers2 = { 10, 20, 30, 40, 50 };

        Console.WriteLine("Access from the end:");
        Console.WriteLine(numbers2[^1]); // 50
        Console.WriteLine(numbers2[^2]); // 40
        Console.WriteLine();

        // 4) Sum all values
        int sum = 0;

        for (int i = 0; i < numbers2.Length; i++)
        {
            sum += numbers2[i];
        }

        Console.WriteLine("Sum:");
        Console.WriteLine(sum);

        Console.ReadKey();
    }
}
Enter fullscreen mode Exit fullscreen mode

0. The Real Goal of This Lesson

An array is not “multiple variables.”
It is one structure that stores many values of the same type under a single reference.

If you miss this:

  • Index logic feels random
  • Off-by-one bugs keep happening
  • Runtime errors feel “mysterious”

This lesson is about understanding the execution model.


1. What Is an Array?

A variable is one box:

int number = 10;
Enter fullscreen mode Exit fullscreen mode

An array is multiple boxes as one structure:

int[] numbers = new int[3];
Enter fullscreen mode Exit fullscreen mode

Meaning:

  • Allocate space for 3 int values
  • Each element is filled with the default value (0)

2. Index Starts at 0

If the array size is 3:

index: 0 1 2
Enter fullscreen mode Exit fullscreen mode

The last valid index is always:

Length - 1
Enter fullscreen mode Exit fullscreen mode

This is non-negotiable.


3. Compile-Time Error vs Runtime Error

Compile-Time Error (Type Mismatch)

numbers[0] = "hello"; // invalid type
Enter fullscreen mode Exit fullscreen mode

The compiler blocks execution.

The program cannot run.


Runtime Error (Index Out of Range)

numbers[3] = 100; // index out of range
Enter fullscreen mode Exit fullscreen mode

Compilation succeeds.

The program starts.

Then it crashes during execution.

Typical error:

Index was outside the bounds of the array
Enter fullscreen mode Exit fullscreen mode

This is the exact difference:

  • Type mistakes fail early (compile-time)
  • Index mistakes fail late (runtime)

4. The Most Common Beginner Mistake

numbers[numbers.Length]
Enter fullscreen mode Exit fullscreen mode

If Length = 3, valid indices are:

0, 1, 2

So numbers[3] is invalid.

Rule:

Last index = Length - 1


5. The ^ Operator (Index from End)

numbers2[^1]
Enter fullscreen mode Exit fullscreen mode

Meaning:

  • Last element

Equivalent to:

numbers2[numbers2.Length - 1]
Enter fullscreen mode Exit fullscreen mode

So:

  • ^1 → last
  • ^2 → second last

It is a convenience feature.

The indexing rules are still the same.


6. The One Rule You Must Not Break in for Loops

Correct:

i < numbers.Length
Enter fullscreen mode Exit fullscreen mode

Incorrect:

i <= numbers.Length
Enter fullscreen mode Exit fullscreen mode

Why?

Because the final iteration will produce:

i == Length
Enter fullscreen mode Exit fullscreen mode

And numbers[Length] is out of range.

This is the classic off-by-one error.


7. The Biggest Limitation of Arrays

Arrays have fixed size.

new int[3]
Enter fullscreen mode Exit fullscreen mode

Those 3 slots stay 3 slots.

You cannot grow or shrink the array.

So:

  • Fixed count → array
  • Variable count → later use List<T>

Final Summary

An array is an index-based structure that stores a fixed number of same-type values.


Learning Check (One Question)

Explain exactly when the runtime error occurs, step by step, tracking i:

int[] numbers = { 10, 20, 30 };

for (int i = 0; i <= numbers.Length; i++)
{
    Console.WriteLine(numbers[i]);
}
Enter fullscreen mode Exit fullscreen mode

You must answer:

  • How does i change starting from 0?
  • What is numbers.Length here?
  • What is the final value of i?
  • At which exact moment does the crash happen, and why?

Top comments (0)