Coding/Learn/csharp/arrays and indexing
C# Foundational Course
01.Why C# & .NET Power Modern Software
02.Anatomy of a C# Application
03.Variables & The C# Type System
04.Console I/O: Reading & Writing
05.String Interpolation & Modern Formatting
06.Arithmetic, Math Class & Modulo
07.Conditional Branching: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements & Modern Switch Expressions
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Methods: Modular, Reusable Code Blocks
14.Optional Parameters, Named Arguments & out/ref
15.Fixed-Size Arrays & Zero-Based Indexing
16.The Sacred Foreach Loop
17.Dynamic Collections: List<T> & Generics
18.Classes & Objects: The OOP Blueprint
19.Auto-Properties & Constructors
20.Capstone: RPG Character & Inventory Engine
Course Certificate0%

Complete all lessons to earn

LearnFixed-Size Arrays & Zero-Based Indexing
15 / 20 0%
Lesson 15 of 20
5 mins read

Fixed-Size Arrays & Zero-Based Indexing

PreviousNext Lesson
In Plain English

An array is a fixed-size collection of items of the same data type stored in contiguous memory. You access individual elements in O(1) instant time using their zero-based index: items[0], items[1].

The Mental Model

"An array is like a row of numbered mailboxes outside a post office. Mailbox 0, Mailbox 1, Mailbox 2... Each box is identical in size and holds mail for one specific recipient."

Deep Dive: How It Works

Declaration: int[] scores = new int[5]; (creates an array of 5 integers, all defaulting to 0).

Initialization Literal: string[] names = { "Neo", "Trinity", "Morpheus" };.

Accessing & Modifying: names[0] = "Thomas"; reads and overwrites the first element.

Array Length (.Length): scores.Length returns the total number of elements.

Index from End (^1 in modern C#): names[^1] accesses the last element cleanly!

Syntax Blueprint

int[] nums = { 10, 20, 30 };
Console.WriteLine(nums[0]); // 10
nums[1] = 99;               // Replaces 20 with 99
int count = nums.Length;    // 3

Array declaration, zero-based indexing, and .Length property.

PointCore Rules to Remember

Point star
Zero-Based Indexing: First element is at index [0]; last element is at [Length - 1].
Point star
Fixed Size: Arrays cannot grow or shrink once allocated in memory.
Point star
IndexOutOfRangeException: Accessing an invalid index throws a runtime exception.

Common Beginner Traps & How to Fix Them

Mistake:Accessing arr[5] on an array of length 5 (new int[5])

Why it happens: Forgetting that indices run from 0 to 4.

How to fix: Valid indices for length 5 are [0], [1], [2], [3], and [4].

Pro Tip: In C# 8+, you can slice arrays using ranges: var firstTwo = names[0..2];!

Live Interactive Example

Hit Run Code to see it live
Array Traversal and Calculations
C# 12 / .NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Output Console
Click "Run Code" to view the rendered output.
How it works: Iterates through indices 0 to scores.Length - 1 and computes the average.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Find the largest number in an array

  • PointDeclare int[] numbers = { 15, 62, 38 };
  • PointFind the maximum value and print "Max: 62".
1
2
3
4
5
6
7
8
9
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates