fork download
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. // The MyClass type has a Finalize method defined for it
  6. // Creating a MyClass places a reference to obj on the finalization table.
  7. var myClass = new MyClass();
  8.  
  9. // Append another 2 references for myClass onto the finalization table.
  10. System.GC.ReRegisterForFinalize(myClass);
  11. System.GC.ReRegisterForFinalize(myClass);
  12. // There are now 3 references to myClass on the finalization table.
  13.  
  14. System.GC.SuppressFinalize(myClass);
  15. System.GC.SuppressFinalize(myClass);
  16. System.GC.SuppressFinalize(myClass);
  17.  
  18. // Remove the reference to the object.
  19. myClass = null;
  20.  
  21. // Force the GC to collect the object.
  22. System.GC.Collect(2, System.GCCollectionMode.Forced);
  23.  
  24. // The first call to obj's Finalize method will be discarded but
  25. // two calls to Finalize are still performed.
  26. }
  27. }
  28.  
  29. class MyClass
  30. {
  31. ~MyClass()
  32. {
  33. System.Console.WriteLine("Finalise() called");
  34. }
  35. }
Success #stdin #stdout 0.03s 36720KB
stdin
Standard input is empty
stdout
Standard output is empty