using System; class A { ~A() { Console.WriteLine("A's destructor"); } } class B: A { ~B() { Console.WriteLine("B's destructor"); } } class Test { static void Main() { B b = new B(); b = null; GC.Collect(); GC.WaitForPendingFinalizers(); } }is
B's destructor A's destructorsince destructors in an inheritance chain are called in order, from most derived to least derived. end example] 5 Destructors may be implemented by overriding the virtual method Finalize on System.Object. 6 In any event, C# programs are not permitted to override this method or call it (or overrides of it) directly. [Example: For instance, the program
class A { override protected void Finalize() {} // error public void F() { this.Finalize(); // error } }contains two errors. end example] 7 The compiler behaves as if this method, and overrides of it, does not exist at all. [Example: Thus, this program:
class A { void Finalize() {} // permitted }is valid and the method shown hides System.Object's Finalize method. end example] 8 For a discussion of the behavior when an exception is thrown from a destructor, see §23.3.
| |
Jagger Software Ltd | |
Company # 4070126 | |
VAT # 762 5213 42 |