C# Academy · Lesson

Introduction to Arrays

Learn how to store and manage multiple data items using arrays.

Lesson 4 of 510 steps

Introduction to Arrays is a free C# Academy lesson on CoddyKit. This is lesson 4 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 5 lessons in total.

Storing Data with Arrays

Introduction to Arrays

Welcome to the next lesson! In this lesson, you’ll learn how to work with arrays in C# to store and manage multiple values efficiently. Let’s get started!

Introduction to Arrays — illustration 1

What Are Arrays?

An array is a collection of elements of the same type, stored in a contiguous block of memory. Arrays allow you to group related data together and access them using an index.

Example: A list of student grades or names can be stored in an array.

Key Point: Arrays in C# are zero-indexed, meaning the first element is at index 0.

Declaring and Initializing Arrays

You can declare and initialize an array in a single step. Example:

Tip: Use square brackets [] to access array elements by their index.

using System;

class Program {
    static void Main() {
        int[] numbers = { 1, 2, 3, 4, 5 }; // Declare and initialize an array
        Console.WriteLine("First number: " + numbers[0]); // Access the first element
    }
}

Accessing Array Elements

Array elements are accessed using their index. Example:

Key Point: Ensure the index is within the bounds of the array to avoid runtime errors.

using System;

class Program {
    static void Main() {
        string[] names = { "Alice", "Bob", "Charlie" };
        Console.WriteLine("First name: " + names[0]); // Outputs: Alice
        Console.WriteLine("Second name: " + names[1]); 
// Outputs: Bob
        Console.WriteLine("Third name: " + names[2]); // Outputs: Charlie
    }
}

Modifying Array Elements

You can modify the value of an array element by assigning a new value to it. Example:

Tip: Use this feature to update values dynamically in your program.

using System;

class Program {
    static void Main() {
        int[] scores = { 10, 20, 30 };

        scores[1] = 25; // Modify the second element
        Console.WriteLine("Updated second score: " + scores[1]); // Outputs: 25
    }
}

Iterating Over Arrays

Use loops like for or foreach to iterate through an array. Example:

Key Point: The foreach loop is simpler for reading array values, but the for loop provides access to the index.

using System;

class Program {
    static void Main() {
        int[] numbers = { 10, 20, 30, 40 };

        foreach (int number in numbers) {
            Console.WriteLine(number); // Outputs each number
        }
    }
}

Multi-Dimensional Arrays

Multi-dimensional arrays store data in a grid-like structure. Example:

Tip: Use commas inside brackets to declare the dimensions of the array.

using System;

class Program {
    static void Main() {
        int[,] matrix = { 
            { 1, 2, 3 }, 
            { 4, 5, 6 } 
        };

        Console.WriteLine("Element at (0,1): " + matrix[0, 1]); // Outputs: 2
    }
}

Practice Challenge

Write a program that stores 5 numbers in an array, then calculates and prints their total. Example:

using System;

class Program {
    static void Main() {
        int[] numbers = { 5, 10, 15, 20, 25 };
        int total = 0;

        foreach (int number in numbers) {
            total += number; // Add each number to the total
        }

        Console.WriteLine("Total: " + total); // Outputs: Total: 75
    }
}

Great Job!

Congratulations! You’ve learned how to declare, access, modify, and iterate through arrays in C#. In the next lesson, we’ll explore working with strings and their powerful features. Let’s keep coding!

Introduction to Arrays — illustration 10
Free to start

Learn C# with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
93
Lessons
346

Frequently Asked Questions

Is the “Introduction to Arrays” lesson free?

Yes — the full text of “Introduction to Arrays” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 5 lessons in total.

What will I learn in “Introduction to Arrays”?

Learn how to store and manage multiple data items using arrays. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 5.

How long does the “Introduction to Arrays” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Control Flow: If-Else Statements
  2. Loops in C#: For, While, and Do-While
  3. Switch Statements
  4. Introduction to Arrays
  5. Working with Strings
← Back to C# Academy