CATEGORII DOCUMENTE |
A destructor is a member that implements the actions required to destruct an instance of a class. A destructors is declared using a destructor-declaration:
destructor-declaration:
attributesopt ~ identifier
( ) block
A destructor-declaration may include a set of attributes (17).
The identifier of a destructor-declarator must name the class in which the destructor is declared. If any other name is specified, an error occurs.
The block of a destructor declaration specifies the statements to be executed in order to destruct an instance of the class. This corresponds exactly to the block of an instance method with a void return type (10.5.8).
Destructors are not inherited. Thus, a class has no destructors other than the one which may be declared in it.
Since a destructor is required to have no parameters, it cannot be overloaded. Thus, a class can have, at most, one destructor.
Destructors are invoked automatically, and cannot be invoked explicitly. An instance becomes eligible for destruction when it is no longer possible for any code to use the instance. Execution of the destructor for the instance may occur at any time after the instance becomes eligible for destruction. When an instance is destructed, the destructors in its inheritance chain are called, in order, from most derived to least derived.
Issue
Describe what happens when an exception is thrown from a destructor.
The output of the example
class A
}
class B:
A
}
public
class Test
}
is
B's
destructor
A's destructor
since destructors in an inheritance chain are called in order, from most derived to least derived.
Implementation note
In CLR, destructors are implemented by overriding the virtual method Finalize on System.Object. C# programs are not permitted to override this method or call it (or overrides of it) directly. For instance, the program
class A
// error
public void F()
}
contains two errors.
The compiler behaves as if this method, and overrides of it, do not exist at all. Thus, this progam:
class A
//
legal
}
is valid, and the method shown hides System.Object's Finalize method.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 916
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved