// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace November { public class Program { public class ManagedData { public ManagedData() { Console.WriteLine("ManagedData memory allocated - GC will deallocate"); } } public class NativeResource : IDisposable { public NativeResource() { Console.WriteLine("NativeResource allocated"); } public void Dispose() { Console.WriteLine("NativeResource deallocated"); } } public class MyException : Exception { } public static void M3() { ManagedData o = new ManagedData(); Console.WriteLine("**** Error ****"); throw new MyException(); } public static void M2() { using(NativeResource o = new NativeResource()) { M3(); Console.WriteLine("Normal return"); } } public static void M1() { ManagedData o = new ManagedData(); M2(); Console.WriteLine("Normal return"); } public static void Main(string[] args) { try { M1(); Console.WriteLine("Normal return"); } catch(MyException) { Console.WriteLine("GC will deallocate all memory"); } } } }