Arrays store fixed-size ordered values, and an index selects one position.

array index An array index is a zero-based number that selects one array element.

Array Indexing

index
ArrayIndexing.cs
Replay: real traced execution (multi-file project)
using System;

class Program
{
    static void Main()
    {
        int index = 1;
        string[] colors = { "red", "green", "blue" };
        string selected = colors[index];
        int count = colors.Length;

        Console.WriteLine($"index={index}");
        Console.WriteLine($"selected={selected}");
        Console.WriteLine($"count={count}");
    }
}
using System;

class Program
{
    static void Main()
    {
        int index = 0;
        string[] colors = { "red", "green", "blue" };
        string selected = colors[index];
        int count = colors.Length;

        Console.WriteLine($"index={index}");
        Console.WriteLine($"selected={selected}");
        Console.WriteLine($"count={count}");
    }
}
using System;

class Program
{
    static void Main()
    {
        int index = 2;
        string[] colors = { "red", "green", "blue" };
        string selected = colors[index];
        int count = colors.Length;

        Console.WriteLine($"index={index}");
        Console.WriteLine($"selected={selected}");
        Console.WriteLine($"count={count}");
    }
}
  1. index ← 1, selected ← green, count ← 3

    4{5    static void Main()6    {7        int index→ 1 = 1; //@index=0, 28        string[] colors = { "red", "green", "blue" };9        string selected→ green = colors[index]green;10        int count→ 3 = colors.Length3;1112        Console.WriteLine($"index={index1}");13        Console.WriteLine($"selected={selectedgreen}");14        Console.WriteLine($"count={count3}");15    }
    outputindex=1
    selected=green
    count=3
  1. index ← 0, selected ← red, count ← 3

    4{5    static void Main()6    {7        int index→ 0 = 0;8        string[] colors = { "red", "green", "blue" };9        string selected→ red = colors[index]red;10        int count→ 3 = colors.Length3;1112        Console.WriteLine($"index={index0}");13        Console.WriteLine($"selected={selectedred}");14        Console.WriteLine($"count={count3}");15    }
    outputindex=0
    selected=red
    count=3
  1. index ← 2, selected ← blue, count ← 3

    4{5    static void Main()6    {7        int index→ 2 = 2;8        string[] colors = { "red", "green", "blue" };9        string selected→ blue = colors[index]blue;10        int count→ 3 = colors.Length3;1112        Console.WriteLine($"index={index2}");13        Console.WriteLine($"selected={selectedblue}");14        Console.WriteLine($"count={count3}");15    }
    outputindex=2
    selected=blue
    count=3

Pick One Slot

  1. The array stores three colors in order.
  2. Indexes start at 0, not 1.
  3. index is 1, so the code selects the second color.
  4. Length reports how many items the array contains. | Index | Color | Selected? | | --- | --- | --- | | 0 | red | no | | 1 | green | yes | | 2 | blue | no |

Exercise: ArrayIndexing.cs

Select one color by index, print the selected color, and print the array length