C#.NET
arrays
programming
add item
coding tutorial

Add new item in existing array in c.net

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In C#, an array has a fixed length once it is created. That means you cannot truly append a new element to the same array object. You either create a new array with more space, or you use a dynamic collection such as List<T> that is designed for growth.

Arrays Are Fixed Size

This is the core rule behind the question.

csharp
int[] numbers = new[] { 1, 2, 3 };
Console.WriteLine(numbers.Length);

After creation, numbers.Length cannot be increased on that instance. Any operation that looks like "adding" to an array is really allocating a new array and copying values.

Use Array.Resize If You Must Keep An Array

If an API truly requires an array, Array.Resize is the simplest built-in option.

csharp
1int[] numbers = new[] { 1, 2, 3 };
2Array.Resize(ref numbers, numbers.Length + 1);
3numbers[^1] = 4;
4
5Console.WriteLine(string.Join(", ", numbers));

This works because Array.Resize creates a larger array internally, copies the old values, and updates the reference.

Manual Copying Shows What Is Really Happening

You can do the same operation explicitly.

csharp
1int[] oldValues = new[] { 1, 2, 3 };
2int[] newValues = new int[oldValues.Length + 1];
3
4Array.Copy(oldValues, newValues, oldValues.Length);
5newValues[^1] = 4;
6
7oldValues = newValues;
8Console.WriteLine(string.Join(", ", oldValues));

This version is useful for understanding the cost: every append requires allocation and copying.

Prefer List<T> For Repeated Growth

If you are adding items repeatedly, List<T> is usually the correct data structure.

csharp
1var numbers = new List<int> { 1, 2, 3 };
2numbers.Add(4);
3numbers.Add(5);
4
5Console.WriteLine(string.Join(", ", numbers));

List<T> manages an internal array for you and grows capacity in chunks, which is much more efficient than resizing an array on every append.

If you later need an array, convert at the boundary:

csharp
int[] result = numbers.ToArray();

That keeps most of your code on the collection type that actually matches the workflow.

When Arrays Still Make Sense

Arrays are still a good fit when the size is known in advance, performance is predictable, and random indexed access matters. They are not the wrong tool in general; they are just the wrong tool for repeated append-heavy workloads.

For example, reading a fixed-size buffer or storing a known number of pixel values is a natural array use case. Building a growable list of user selections is not.

Performance Tradeoffs

Resizing an array once is fine. Resizing it in a tight loop is expensive because each growth step copies all existing elements.

csharp
1int[] values = Array.Empty<int>();
2for (int i = 0; i < 1000; i++)
3{
4    Array.Resize(ref values, values.Length + 1);
5    values[^1] = i;
6}

This works, but it is much slower than:

csharp
1var values = new List<int>();
2for (int i = 0; i < 1000; i++)
3{
4    values.Add(i);
5}

The second version is the one you usually want.

Common Pitfalls

The most common mistake is thinking an array can be extended in place. It cannot. Another is using Array.Resize in a loop that keeps appending one item at a time, which creates unnecessary copying overhead. Developers also sometimes keep everything as List<T> and never convert back to an array even when a downstream API requires one; the clean answer is to use ToArray() at that boundary. Finally, if several variables point to the same original array, remember that resizing reassigns the reference rather than mutating the old instance.

Summary

  • C# arrays have fixed length and cannot truly grow in place.
  • 'Array.Resize works by allocating a new array and copying data.'
  • Manual copying is equivalent, just more explicit.
  • 'List<T> is the better choice when you need to add items repeatedly.'
  • Convert to an array only when an API or storage format actually requires it.

Course illustration
Course illustration

All Rights Reserved.