# But Arrays Can Be Tedious

Arrays can't grow or shrink, so adding, inserting and removing elements means making new arrays and shifting values by hand.

Apprentice · C# · Updated November 18, 2026

By Gregor Dzierzon

## After this lesson you can

- Explain why arrays are awkward when items are added or removed
- Grow or shrink an array with Array.Resize, and reset its elements with Array.Clear

## Key takeaways

- An array's size is fixed when it's created. To add an element, you make a new, bigger array and copy every element across.
- Inserting in the middle of an array means shifting every later element one place right; removing means shifting them left to close the hole.
- .NET's collections, such as `List<T>` and `Dictionary<TKey, TValue>`, do the growing, shifting and copying for you.
- Use an array for a group that stays the same size, and a collection for data that grows and shrinks.
- `Array.Resize(ref cart, 4)` still makes a new array and copies, and `Array.Clear` sets elements back to 0, `false` or `null` without changing the array's length.

Arrays are excellent at finding a slot. But real data comes and goes: a shopping cart gains items, a waiting list loses names. That's where arrays get tedious.

## You can't stretch an array

An array's size is fixed when it's created. To add one more element to a full array, you must create a new, bigger array, and copy every element across:

```csharp
string[] cart = { "milk", "eggs", "bread" };

// Adding a fourth item: make a bigger array and copy everything
string[] biggerCart = new string[cart.Length + 1];
for (int i = 0; i < cart.Length; i++)
{
    biggerCart[i] = cart[i];
}
biggerCart[cart.Length] = "butter";
cart = biggerCart;

Console.WriteLine(string.Join(", ", cart));
```

```text
milk, eggs, bread, butter
```

Five lines of work to add one item, every time.

## Inserting in the middle

To put an element in the middle, everything after that position has to shift one place to the right, to make a gap, and that's on top of making the array bigger.

![An array with an empty slot in the middle, and the elements after it shifting right](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/4f16ba875c3342cbbb3510dbfa527f6f.webp)

## Removing from the middle

Taking an element out leaves a hole. To close it, every element after it shifts one place to the left, and you're left with an unused slot at the end, or another copy into a smaller array.

```csharp
string[] waitingList = { "Ana", "Raj", "Lee", "Kim" };

// Remove "Raj" (index 1): shift everything after it to the left
for (int i = 1; i < waitingList.Length - 1; i++)
{
    waitingList[i] = waitingList[i + 1];
}
waitingList[waitingList.Length - 1] = "";

Console.WriteLine(string.Join(", ", waitingList));
```

```text
Ana, Lee, Kim,
```

The last slot is now an empty string the program has to remember to ignore.

## Two helpers: Array.Resize and Array.Clear

Copying a whole array by hand every time the cart grows is a lot of loop to get right. .NET's `Array` class has a method that does those steps for you, and another that empties an array's slots without making a new one.

**`Array.Resize`** makes a new array of the size you ask for, copies the elements across, and puts the new array in your variable:

```csharp
string[] cart = { "milk", "eggs", "bread" };

Array.Resize(ref cart, cart.Length + 1);
cart[cart.Length - 1] = "butter";

Console.WriteLine(string.Join(", ", cart));
Console.WriteLine(cart.Length);
```

```text
milk, eggs, bread, butter
4
```

The new place is empty until you fill it, so the second line puts `"butter"` in the last slot. A smaller size shrinks the array instead, and the elements past the new end are left behind: `Array.Resize(ref waitingList, 2)` keeps only the first two names.

Two things to notice:

- **It's the same work, in fewer lines.** An array still can't stretch: `Resize` makes a brand-new array and copies every element, exactly as the loop in *You can't stretch an array* does. Resizing a big array again and again is still slow.
- **`ref` in the call.** `Resize` has to change *your* variable, so that `cart` holds the new, bigger array afterward. A method can only change the caller's variable when the argument is passed with `ref`, which *Passing Values to a Method: by Value and by Reference* teaches. Leave out `ref`, and the code doesn't compile.

**`Array.Clear`** sets elements back to their type's **default value**: `0` for numbers, `false` for `bool`, and `null`, no string at all, for `string`. The array keeps its length. After each game, the scoreboard's quarter scores go back to 0:

```csharp
int[] quarterScores = { 7, 3, 14, 10 };

Array.Clear(quarterScores, 2, 2);
Console.WriteLine(string.Join(", ", quarterScores));

Array.Clear(quarterScores);
Console.WriteLine(string.Join(", ", quarterScores));
Console.WriteLine(quarterScores.Length);
```

```text
7, 3, 0, 0
0, 0, 0, 0
4
```

- `Array.Clear(quarterScores, 2, 2)` clears 2 elements, starting at index 2: a start and a count, like `Substring`.
- `Array.Clear(quarterScores)` clears every element.

`Clear` needs no `ref`: it changes the elements inside the array you already have, not which array your variable holds.

## Every program needs this

Growing, inserting and removing are such common jobs that writing the shifting and copying by hand, again and again, would be a waste of time and a source of bugs. So .NET provides **collections** that do it for you. The next lessons meet the two you'll use most: `List<T>` and `Dictionary<TKey, TValue>`.

## Do it: seat the first person on the waiting list

The restaurant keeps its waiting list in an array of names, in the order people arrived. When a table opens, the first person on the list is seated, and the list gets one name shorter. An array can't shrink, so the method has to build a new, shorter array.

The method gets one value:

- `waitingList`: a `string[]` of names, the first to arrive first. It always has at least one name.

For example:

```csharp
string[] waitingList = { "Ana", "Raj", "Lee", "Kim" };
```

Ana is seated. The new array has 3 places, and each place gets the name one place further along in the old array:

| New array's place | Copied from | Name |
|---|---|---|
| 0 | `waitingList[1]` | Raj |
| 1 | `waitingList[2]` | Lee |
| 2 | `waitingList[3]` | Kim |

So `RemoveFirst(waitingList)` returns `{ "Raj", "Lee", "Kim" }`.

**Your job:** write the method `RemoveFirst(string[] waitingList)` so it returns a new `string[]`.

- The new array is one element shorter than `waitingList`.
- It holds every name but the first, in the same order.
- When the list has only one name, the new array is empty.
- `waitingList` itself stays as it was: don't change it.

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

## Do it: add a name to the end of the waiting list

When someone new arrives at the restaurant, their name goes at the end of the waiting list. The list is an array, so it has to grow by one place first.

The method gets two values:

- `waitingList`: a `string[]` of names, the first to arrive first. It can be empty.
- `name`: the name of the person who just arrived, a `string` (`"Kim"`).

For example, with `{ "Ana", "Raj", "Lee" }` and `"Kim"`:

| Step | The array |
|---|---|
| Before | `{ "Ana", "Raj", "Lee" }` (3 places) |
| Resized to one place more | `{ "Ana", "Raj", "Lee", null }` (4 places) |
| Kim in the last place | `{ "Ana", "Raj", "Lee", "Kim" }` |

So `AddToWaitingList(new string[] { "Ana", "Raj", "Lee" }, "Kim")` returns `{ "Ana", "Raj", "Lee", "Kim" }`.

**Your job:** write the method `AddToWaitingList(string[] waitingList, string name)` so it returns the longer `string[]`.

- Grow the array by one place with `Array.Resize`.
- Put `name` in the new last place.
- Return the array.
- An empty list gets its first name: `AddToWaitingList(new string[] { }, "Ana")` returns `{ "Ana" }`.

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

Arrays are perfect for fixed groups, and painful for groups that change. When your data grows and shrinks, reach for a collection that manages the array for you.

## Key terms

- [Collection](https://www.ask-codey.com/glossary/collection)

## Cheat sheets

- [But Arrays Can Be Tedious](https://www.ask-codey.com/cheat-sheets/array-review-in-csharp-but-arrays-can-be-tedious-cheat-sheet)

Source: https://www.ask-codey.com/lessons/array-review-in-csharp-but-arrays-can-be-tedious
