Home C# Arrays Methods Resize() C# Array Resize() Method Definition In C#, Array.Resize changes which array your variable points to. It allocates a new array with the new length, copies elements from the old array, and writes that new reference back through a ref parameter.
Arrays themselves still have fixed length. Resize is sugar for “make a bigger/smaller array and keep my variable updated.” Prefer List<T> when you resize often.
This is a static member of System.Array. You call it with the class name, such as Array.Resize(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.
Syntax Pass the array variable with ref and the new size.
Array.Resize(ref nums, 8);
Array.Resize(ref nums, 2); Call What it does Array.Resize(ref nums, 8)New length 8; old values copied; new slots are default. Array.Resize(ref nums, 2)New length 2; only the first two old values are kept.
Other variables that still reference the old array will not see the resize.
Library declarations public static void Resize<T>(ref T[] array, int newSize); Parameters Parameter Description array Required. Passed by ref so the caller variable can be replaced. newSize Required. New length (non-negative).
Return Value Return Value Description void The ref variable now references the new array.
C# Examples 1. Grow 2. Shrink 3. Old alias Example 1 — Grow an Array Expand length from 3 to 5 and show default-filled slots.
using System;
class Program {
static void Main() {
int[] nums = { 1, 2, 3 };
Array.Resize(ref nums, 5);
Console.WriteLine("Length: " + nums.Length);
Console.WriteLine(string.Join(", ", nums));
}
} How It Works ref required. Without ref, the caller could not see the new array.Copy prefix. 1, 2, 3 move into the new buffer.Defaults. New int slots are 0.New object. Length is now 5 on the replacement array.Example 2 — Shrink and Truncate Reduce length and drop trailing elements.
using System;
class Program {
static void Main() {
string[] names = { "Ann", "Bob", "Cara", "Dan" };
Array.Resize(ref names, 2);
Console.WriteLine(string.Join(", ", names));
Console.WriteLine("Length: " + names.Length);
}
} How It Works Keep prefix. Only the first two names survive.Cara/Dan dropped. They are not in the new array.Length updates. Variable now reports 2.Data loss. Shrinking discards the tail permanently (for this variable).Example 3 — Old Reference Unchanged Show that another variable still points at the pre-resize array.
using System;
class Program {
static void Main() {
int[] nums = { 1, 2, 3 };
int[] alias = nums;
Array.Resize(ref nums, 5);
Console.WriteLine("nums.Length = " + nums.Length);
Console.WriteLine("alias.Length = " + alias.Length);
}
} nums.Length = 5
alias.Length = 3 How It Works Shared at first. alias and nums began as one object.Resize retargets nums. Only the ref parameter is updated.alias stays old. It still has length 3.Mind aliases. This surprises people coming from resizable lists.Frequently Asked Questions Does Resize grow the same array object? No. It allocates a new array and updates your ref variable to point at it.
Why is ref required? So the method can replace the caller’s array reference with the new array.
What happens to new slots when growing? They are set to default(T): 0 for int, null for reference types.
What happens when shrinking? Only elements that fit in the new length are copied; the rest are discarded from the new array.
Should I Resize in a loop? Prefer List<T> for frequent growth. Resize allocates each time.
Next: Reverse() Reverse the order of elements in an array (or a range) in place with System.Array.
Reverse() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
7 people found this page helpful
Helpful Share Copy link Suggestion