public static void Main() { SomeResource a = new SomeResource("First"); a.Consume(); Run(); Console.WriteLine("Before garbage-collection {0} resource is in generation:{1}", a.Id, GC.GetGeneration(a)); GC.Collect(); //forcing garbage-collection GC.WaitForPendingFinalizers(); Console.WriteLine("After garbage-collection {0} resource is in generation:{1}", a.Id, GC.GetGeneration(a)); SomeResource c = new SomeResource("Third"); c.Consume(); c.Dispose(); using (SomeResource d = new SomeResource("Fourth")) { d.Consume(); } }
private static void Run() { SomeResource b = new SomeResource("Second"); b.Consume(); }