.NET 通过垃圾收集器(GC)来自动管理内存 — 它在托管堆上分配对象并回收不再可达的对象,因此您无需手动释放内存。理解 GC(分代式、栈/堆划分和 IDisposable)对性能和正确性很重要。
栈 vs 堆
Stack → value types (locals), method frames; fast, automatically freed when scope ends.
Heap → reference type objects (class instances); managed by the GC.
A reference variable lives on the stack but points to its object on the heap.
