Thuta Learning
C#
IntermediateProgrammingBeginner

Arrays

C#Lesson 1220 min

What you'll walk away with

  • Explain the core concept and runtime behavior of Arrays
  • Test normal, boundary, and invalid cases independently
  • Write readable, type-safe, and testable C# code

Explain it simply

Arrays, in plain language

Array is a fixed-size collection that stores multiple values of the same type in one place. Array indexes start at 0 , so to get the first item you write array[0] .

Problem → Why → Solution

The problem — why this concept matters

Treating `Length` as the last valid index causes `IndexOutOfRangeException`.

A common mistake

Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Core concept — understand how it works

An array is a fixed-length, zero-based collection of one element type. Understand index bounds, shared references, multidimensional or jagged shapes, and the trade-off between arrays and `List<T>`.

Mental model
  1. typed input
  2. C# expression and method
  3. CLR value or object state

Explain the core concept and runtime behavior of Arrays

Core syntax or pattern

csharp
string[] courses = { "C#", "JavaScript", "PHP", "Java" };

Console.WriteLine(courses[0]);

courses[1] = "TypeScript";
Console.WriteLine(courses[1]);

Console.WriteLine($"Total courses: {courses.Length}");

An array is a fixed-length, zero-based collection of one element type. Understand index bounds, shared references, multidimensional or jagged shapes, and the trade-off between arrays and `List<T>`.

Read the code step by step

  1. typed inputstring[] courses = { "C#", "JavaScript", "PHP", "Java...

    Array is a fixed-size collection that stores multiple values of the same type in one place. Array indexes start at 0 , so to get the first item you write array[0] .

  2. C# expression and methodcourses[1] = "TypeScript";

    An array is a fixed-length, zero-based collection of one element type. Understand index bounds, shared references, multidimensional or jagged shapes, and the trade-off between arrays and `List<T>`.

  3. CLR value or object stateConsole.WriteLine($"Total courses: {courses.Length}");

    Demonstrate an empty array, first and last indexes, copying versus a shared reference, and out-of-range handling.

Execution flow
  1. typed input
  2. C# expression and method
  3. CLR value or object state

Success

Explain the core concept and runtime behavior of Arrays

Failure

Treating `Length` as the last valid index causes `IndexOutOfRangeException`.

Array is a fixed-size collection that stores multiple values of the same type in one place. Array indexes start at 0, so to get the first item you write array[0].

csharp
string[] courses = { "C#", "JavaScript", "PHP", "Java" };

Console.WriteLine(courses[0]);

courses[1] = "TypeScript";
Console.WriteLine(courses[1]);

Console.WriteLine($"Total courses: {courses.Length}");

Key things to know

  • courses[0] grabs the first item.
  • courses[1] = "TypeScript"; changes the second item.
  • Length gives you back how many items are in the array.
You should see
C# TypeScript Total courses: 4

Info

⚠️ Common mistake

Forget that array indexes start at 0, and grabbing the last item as courses[4] will throw an error. In this example, the last item is actually courses[3].

Real-world usage

How to apply it in a real project

Demonstrate an empty array, first and last indexes, copying versus a shared reference, and out-of-range handling.

Mini challenge

Build it yourself

Demonstrate an empty array, first and last indexes, copying versus a shared reference, and out-of-range handling.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
csharp
string[] courses = { "C#", "JavaScript", "PHP", "Java" };

Console.WriteLine(courses[0]);

courses[1] = "TypeScript";
Console.WriteLine(courses[1]);

Console.WriteLine($"Total courses: {courses.Length}");

Expected: Explain the core concept and runtime behavior of Arrays

Show hint

Demonstrate an empty array, first and last indexes, copying versus a shared reference, and out-of-range handling.

Quick quiz 1 / 3

What best describes the core idea of Arrays?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • typed input → C# expression and method → CLR value or object state
  • An array is a fixed-length, zero-based collection of one element type. Understand index bounds, shared references, multidimensional or jagged shapes, and the trade-off between arrays and `List<T>`.
  • Treating `Length` as the last valid index causes `IndexOutOfRangeException`.
  • Demonstrate an empty array, first and last indexes, copying versus a shared reference, and out-of-range handling.

C# documentation — Microsoft Learn

Easy traps

  • Treating `Length` as the last valid index causes `IndexOutOfRangeException`.
  • Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Hands-on Exercise

Demonstrate an empty array, first and last indexes, copying versus a shared reference, and out-of-range handling.

You'll know it worked when: C# TypeScript Total courses: 4

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Arrays | Thuta Learning