Delphi → C# / Java / C++ Porting Trap
Delphi B := A dynamic array aliasing vs. SetLength detachment.
When an AI coding agent ports Delphi to C# or Java, it usually treats B := A as a single uniform assignment. In Delphi, B := A implements 6 different physical copy/aliasing models across 13 type families.
The 6 physical models of B := A
- Static arrays (
array[0..3] of Integer) & plainrecords: copy by value (in C#,int[]is a reference — a naive port silently introduces aliasing!). - Long strings (
UnicodeString/AnsiString): share the underlying buffer with copy-on-write on element mutation. - Dynamic arrays (
array of T): share the underlying mutable buffer without copy-on-write — mutatingB[0]mutatesA[0]! YetSetLength,Insert,Delete, orCopydetaches onto a fresh buffer even when the length does not change. - Classes (
TObject): non-refcounted reference copy (manual ownership). - Interfaces (
IInterface): deterministic reference-counted handle copy (can triggerDestroyimmediately on overwrite or scope exit). - Managed records / Variants: invoke compiler-generated
Assign/AddRef/Finalizehooks.
Runnable Delphi 13 proof (01-array-alias.dpr)
program ArrayAlias;
{$APPTYPE CONSOLE}
type TNumbers = array of Integer;
var A, B: TNumbers;
begin
SetLength(A, 1);
A[0] := 4;
B := A; // shares buffer WITHOUT copy-on-write
B[0] := 9; // mutates A[0] as well!
Writeln(A[0]); // Output: 9
SetLength(B, Length(B)); // detaches B even with IDENTICAL length!
B[0] := 12;
Writeln(A[0]); // Output: 9
Writeln(B[0]); // Output: 12
end.A naive C# port using List<int> or int[] either breaks the shared write on line 9 or forgets to clone on SetLength(B, Length(B)). Both versions compile cleanly and pass simple happy-path tests, then corrupt production state when two callers share an array.
How to port safely
Audit every dynamic array parameter (const, var, out, value) and every SetLength/Copy/Insert/Delete site as explicit alias vs. detach boundaries before choosing T[], List<T>, or defensive cloning in C#.
Related guides: Logical shr vs. C# >> · Offline Delphi MCP setup.
Verify it yourself in Delphi 13 (Free < 1 MB Demo)
Every rule in these guides comes from 879 build observations and 650 executable runs on Delphi 13 Community Edition (Win32, Win64, and compile-only Android ARM). The free demo on GitHub includes the complete 74-chapter & 89-directive Table of Contents, 6 multi-difficulty excerpts, 4 complete directive cards, 8 compiler JSON receipts, 6 runnable .dpr probes, and working plugins, skills, and local offline MCP server.
Get Full Universal Suite · from $99Inspect Free Demo on GitHubAll Technical Guides
Honest scope boundaries: Strictly a Delphi / Object Pascal language, runtime-behavior, and compiler-directive reference — not a VCL, FMX, or FireDAC component API encyclopedia. Does not claim 100% coverage of the language or any ABI; open questions are listed explicitly by name. Independent product, not affiliated with Embarcadero Technologies, Inc.