13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 17 Classesisusing 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();}}B’s destructorA’s destructorsince destructors in an inheritance chain are called in order, from most derived to least derived. The GC.Collectmethod is not required, but may be provided by an implementation. end example]Destructors may be implemented by overriding the virtual method Finalize on System.Object. In any event,<strong>C#</strong> programs are not permitted to override this method or call it (or overrides of it) directly. [Example: Forinstance, the programclass A{override protected void Finalize() {} // error}public void F() {this.Finalize();}// errorcontains two errors. end example]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]For a discussion of the behavior when an exception is thrown from a destructor, see §23.3.265

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!