.NETはガベージコレクタ(GC)を介してメモリを自動的に管理します。オブジェクトはマネージドヒープに割り当てられ、到達不可能なオブジェクトは自動的に回収されるため、手動でメモリを解放する必要がありません。GC(ジェネレーション、スタック/ヒープの分割、IDisposable)を理解することはパフォーマンスと正確性にとって重要です。
スタック対ヒープ
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.
