← Back to C# Course | Chapter 9: Collections | Lesson 1 of 7

Arrays

In this page:

  1. Arrays

Arrays

An array is a fixed-size, ordered collection of elements of the same type, indexed starting at 0. Once created, its length cannot change. Arrays are the most basic collection type and back many of the more flexible collections internally.

Warning: Accessing an index outside the array's bounds throws an IndexOutOfRangeException at runtime.

Example: Arrays

markup
using System;

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

        Console.WriteLine(numbers.Length);
        Console.WriteLine(numbers[0]);
        numbers[1] = 99;

        foreach (int n in numbers)
        {
            Console.WriteLine(n);
        }
    }
}
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.