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();
}
}
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;
An array is multiple boxes as one structure:
int[] numbers = new int[3];
Meaning:
- Allocate space for 3
intvalues - Each element is filled with the default value (
0)
2. Index Starts at 0
If the array size is 3:
index: 0 1 2
The last valid index is always:
Length - 1
This is non-negotiable.
3. Compile-Time Error vs Runtime Error
Compile-Time Error (Type Mismatch)
numbers[0] = "hello"; // invalid type
The compiler blocks execution.
The program cannot run.
Runtime Error (Index Out of Range)
numbers[3] = 100; // index out of range
Compilation succeeds.
The program starts.
Then it crashes during execution.
Typical error:
Index was outside the bounds of the array
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]
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]
Meaning:
- Last element
Equivalent to:
numbers2[numbers2.Length - 1]
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
Incorrect:
i <= numbers.Length
Why?
Because the final iteration will produce:
i == Length
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]
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]);
}
You must answer:
- How does
ichange starting from 0? - What is
numbers.Lengthhere? - What is the final value of
i? - At which exact moment does the crash happen, and why?
Top comments (0)