# What Arrays Solve

A quick review of what arrays do well, fast access by position, one name for a group, and easy looping, before meeting their limits.

Apprentice · C# · Updated November 15, 2026

By Gregor Dzierzon

## After this lesson you can

- Explain what arrays do well, such as fast access by index and easy looping

## Key takeaways

- An array stores its elements side by side, so reaching any element by its index takes the same short time, whether it's the first or the ten-thousandth.
- An array keeps related values under one name, so you can hand the whole group to a method in one argument.
- A loop can visit every element of an array in order, to search, add up, change or print them.
- `Array.Sort` puts an array's elements in order: alphabetical for text, smallest first for numbers.
- When you know how many values you have, and that number won't change, an array is a good fit.

You've used arrays to hold groups of values: scores, names, prices. Before meeting C#'s other collections, it's worth being clear about what arrays do well, because the others build on them.

## Fast access by position

An array stores its elements side by side, in one block of the computer's memory. To find element 10,000, the computer doesn't search: it works out exactly where it is from the index, and goes straight there. Reaching any element by its index takes the same short time, whether it's the first or the ten-thousandth.

```csharp
int[] attendance = new int[10_000];
attendance[9_999] = 92_746;

Console.WriteLine(attendance[9_999]);
```

```text
92746
```

(Underscores in a number, as in `10_000`, are only there to make it easier to read. C# ignores them.)

## One name for a group

An array keeps related values together under one name. You can hand the whole group to a method in one argument, instead of a long list of separate variables:

```csharp
int[] quarterScores = { 12, 14, 6, 7 };
Console.WriteLine($"Final score: {Total(quarterScores)}");

int Total(int[] scores)
{
    int total = 0;
    foreach (int score in scores)
    {
        total += score;
    }
    return total;
}
```

```text
Final score: 39
```

## Easy to loop over

Because every element has a numbered place, a loop can visit them all, in order, to search, add up, change or print them. Many everyday algorithms, such as finding the largest value or sorting, are designed around exactly this.

```csharp
string[] teams = { "Ohio State", "Georgia", "Clemson", "Alabama" };
Array.Sort(teams);

foreach (string team in teams)
{
    Console.WriteLine(team);
}
```

```text
Alabama
Clemson
Georgia
Ohio State
```

`Array.Sort` puts an array's elements in order: alphabetical for text, smallest first for numbers.

## Do it: count the big crowds

The stadium keeps one number for each home game: how many people came. The ticket office wants to know how many games drew a big crowd.

The method gets two values:

- `attendance`: an `int[]`, how many people came to each game.
- `bigCrowd`: an `int`, the smallest crowd that counts as big.

For example, four games, with 60,000 as a big crowd:

```csharp
int[] attendance = { 41_000, 92_746, 65_300, 88_000 };
int bigCrowd = 60_000;
```

| Game | People | 60,000 or more? |
|---|---|---|
| 1st | 41,000 | no |
| 2nd | 92,746 | yes |
| 3rd | 65,300 | yes |
| 4th | 88,000 | yes |
| **Big crowds** | | **3** |

So `CountBigCrowds(attendance, 60_000)` returns `3`.

**Your job:** write the method `CountBigCrowds(int[] attendance, int bigCrowd)` so it returns, as an `int`, how many games had a big crowd.

- A game counts when its crowd is `bigCrowd` or more. A crowd of exactly `bigCrowd` counts.
- If no game had a big crowd, return `0`.
- An empty array has no games, so it returns `0` too.

Practiced live on the lesson page: write the C# right there and check it.

## Check yourself

1 question to try on the lesson page.

## So what

When you know how many values you have, and that number won't change, an array is fast, simple and hard to beat.

## Key terms

- [Array](https://www.ask-codey.com/glossary/array-c-sharp)
- [Array Index](https://www.ask-codey.com/glossary/array-index-c-sharp)

## Cheat sheets

- [What Arrays Solve](https://www.ask-codey.com/cheat-sheets/array-review-in-csharp-what-arrays-solve-cheat-sheet)

Source: https://www.ask-codey.com/lessons/array-review-in-csharp-what-arrays-solve
