🔧 Fix Code Holding a Reference to an Array Element Silently Pointing at the Old Array After Array.Resize Allocates a New One
Array.Resize looks, from its name, like it grows or shrinks the array in place, but a C# array’s length is fixed at the moment it’s created and can never actually change – what Array.Resize really does is allocate an entirely new array of the requested size, copy over as many elements as fit, and reassign the ref parameter so the variable passed to it now points at that new array instead of the old one. Any other variable, field, or captured closure that was holding a reference to the original array object, rather than to the ref parameter Array.Resize was given, keeps pointing at that original, now-orphaned array exactly as it was before the resize – it doesn’t see the new elements Array.Resize added, and if the resize shrank the array, it still sees the elements that were supposedly removed, because as far as that other reference is concerned, nothing happened to the array it’s holding at all.
🔎 The Problem
public class Buffer
{
public int[] Data = new int[] { 1, 2, 3 };
}
var buffer = new Buffer();
int[] alias = buffer.Data; // alias now references the SAME array object
Array.Resize(ref buffer.Data, 5);
buffer.Data[3] = 40;
buffer.Data[4] = 50;
Console.WriteLine(buffer.Data.Length); // 5 - buffer.Data points at the NEW array
Console.WriteLine(alias.Length); // 3 - alias still points at the OLD array,
// completely unaware a resize ever happened
Console.WriteLine(alias is buffer.Data);
// False in the sense that matters - ReferenceEquals(alias, buffer.Data) is false,
// they are now two genuinely different array objects
✅ Fix: Don’t Keep Separate References to an Array That Might Be Resized
- After calling Array.Resize on a field or variable, re-read that field or variable again anywhere else it’s needed, rather than relying on a reference captured before the resize – that captured reference is now pointing at a stale, disconnected copy of the old data.
- Where practical, prefer a generic List over a raw array for any collection that might need to grow or shrink – a List manages its own internal array and exposes a stable reference to the List object itself, so other code holding a reference to the list sees size changes correctly instead of being silently left behind.
- If an array genuinely must be resized in place conceptually, make that explicit by always accessing it through a single owning object’s property or method rather than ever letting a second variable alias the underlying array directly, so there’s no second reference left around to go stale.
🚨 Why This Is Dangerous
- Code holding what it believes is a live view of an array’s current contents, such as a cached alias inside a closure or a second field, can keep silently working against outdated data indefinitely after a resize, producing results that reflect an array state that technically no longer exists anywhere except in that one stale reference.
Array.Resize keeps its promise to the ONE reference you handed it by ref – every other reference to that same array made its own separate promise to nobody in particular, and Array.Resize never agreed to keep it updated too.

