C # detailed parsing of the constructor used to destroy class instances

Source: Internet
Author: User

Destructor are declared in the following form:
[Attributes] ~ Identifier () {destructor-body}
Where:

Attributes (optional)
Additional declarative information. For more information about attributes and attribute classes, see C # attributes.
Identifier
Identifier and class names are the same.
Destructor-body
The block that contains the statement to destroy the class instance.
Remarks
You cannot use destructor for structures. Only destructor can be used for classes.

A class can have only one destructor.

The Destructor cannot be inherited or overloaded.

You cannot call the destructor. They are automatically called.

The Destructor neither have modifiers nor parameters. For example, the declaration of the destructor of the class MyClass is as follows:

~ MyClass () {// Cleanup statements .}
The Destructor implicitly calls the Object. Finalize method to the base class of the Object. In this way, the previous destructor code is implicitly converted:

Protected override void Finalize () {try {// Cleanup statements.} finally {base. Finalize ();}}
This means that the Finalize method is called recursively for all instances in the inheritance chain (from the largest degree of similarity to the least degree of similarity derived.

Programmers cannot control when to call the Destructor because it is determined by the garbage collector. The garbage collector checks whether there are objects that are no longer used by the application. It considers that these objects meet the destruction conditions and reclaim the memory occupied by these objects. The Destructor is also called when the program exits.

You can call the GC. Collect method to force garbage collection, but this should be avoided in most cases, because this will lead to performance problems. For more information, see force garbage collection.

Example
The following example creates three classes, which constitute an inheritance chain. Class First is a base class, Second is derived from First, and Third is derived from Second. All three classes have destructor. In Main (), an instance of the class with the highest degree of similarity is created. When running the program, note that the destructor of these three classes will be automatically called and called in the order from the largest degree of similarity to the smallest degree of similarity derived.

// Destructors1.csusing System; class First {~ First () {Console. WriteLine ("First's destructor is called");} class Second: First {~ Second () {Console. WriteLine ("Second's destructor is called");} class Third: Second {~ Third () {Console. writeLine ("Third's destructor is called") ;}} public class MainClass {public static void Main () {Third myObject = new Third ();}}
Output
Third's destructor is calledSecond's destructor is calledFirst's destructor is called
Use destructor to release resources
Generally, you do not need to focus on memory management as you do when using C. This is because the. NET Framework Garbage Collector implicitly manages the memory allocation and release of objects. However, when applications encapsulate unmanaged resources such as Windows, files, and network connections, you should use the destructor to release these resources. When the object meets the destruction condition, the garbage collector runs the Finalize method of the object.

Explicit resource release
If your applications are using expensive external resources, we also recommend that you explicitly release resources before the Garbage Collector releases objects. You can achieve this by implementing the Dispose method (from the IDisposable Interface), which is necessary for object cleanup. This greatly improves the application performance. Even with this explicit control over resources, destructor are also a protection measure that can be used to clear resources when a call to the Dispose method fails.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.