Destructor (C #),

Source: Internet
Author: User

Destructor (C #),

Destructor, also known as terminator, is used for destructor instances.

Definition

Destructor is opposite to constructor. When an object ends its lifecycle (for example, the function of the object has been called), the system automatically executes the constructor. Destructor are often used to clean up the aftermath. (For example, when an object is created, a memory space is opened up with new, and delete automatically calls the destructor to release the memory ).

 

 

Introduction to destructor

Take the C ++ language as an example: [1] The Destructor name should also be the same as the class name, except that a single digit is added before the function name to take the inverse character ~, Example ~ Stud () to distinguish it from the constructor. It does not contain any parameters or return values (including void type ). You can only have one destructor and cannot overload it. If you have not compiled the destructor, the compilation system will automatically generate a default destructor (even if you have customized the destructor, the compiler will always synthesize a destructor for us, in addition, if a custom destructor is defined, the compiler will first call the custom destructor before calling the merged destructor during execution. Therefore, explicit destructor are not used in many simple classes.

 

Use of destructor
  • The Destructor cannot be defined in the structure. 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.

 

Statement:

class Car{    ~ Car()  // destructor    {        // cleanup statements...    }}

The Destructor implicitly calls Finalize to the object's base class. In this way, the previous destructor code is implicitly converted:

protected override void Finalize(){    try    {        // cleanup statements...    }    finally    {        base.Finalize();    }}

This means recursively calling all instances in the inheritance chainFinalizeMethod (from the largest degree of derivation to the least degree of derivation ).

Note:

Empty destructor should not be used. If the class contains destructor,FinalizeAn item is created in the queue. When you call the destructor, the garbage collector is called to process the queue. If the Destructor is empty, it only causes unnecessary performance loss.

Programmers cannot control when to call 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. If the Garbage Collector considers an object to conform to the destructor, it calls the Destructor (if any) and recycles the memory used to store the object. The Destructor is also called when the program exits.

You can force garbage collection by calling Collect, but this should be avoided in most cases, because this will cause performance problems. For more information, see force garbage collection.

 

Use destructor to release resources

Generally, C # does not require much memory management compared to programming languages that do not recycle garbage during runtime. 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 destructor, the garbage collector runsFinalizeMethod.

 

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 implementDisposeMethod to complete this, this method is necessary for object cleaning. This greatly improves the application performance. Even with this explicit control over resources, destructor are also a protection measure that can be usedDisposeClear resources when a method call fails.

 

Example

The following example creates three classes, which constitute an inheritance chain. ClassFirstIs a base class,SecondYesfromFirstDerived, andThirdYesfromSecondDerived. All three classes have destructor. InMain (). Note: When the program is running, the destructor of the three classes will be automatically called and called in the order from the largest degree of derivation to the smallest degree of derivation.

class First{    ~First()    {        System.Console.WriteLine("First's destructor is called");    }}class Second: First{    ~Second()    {        System.Console.WriteLine("Second's destructor is called");    }}class Third: Second{    ~Third()    {        System.Console.WriteLine("Third's destructor is called");    }}class TestDestructors{    static void Main()     {        Third t = new Third();    }}

 

 

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.