Detailed description and usage of asp.net destructor

Source: Internet
Author: User

A destructor is a method member that destroys an instance of a class. The Destructor cannot have parameters, any modifiers, and cannot be called. Because the purpose of the Destructor is opposite to that of the constructor, the prefix '~ 'To show the difference.

Although C # (or CLR) provides a new memory management mechanism-Automatic memory management ), resources can be released automatically through the "Garbage Collector". Generally, no user intervention is required. However, in some special circumstances, you still need to use the destructor, for example, release of unmanaged resources in C.

Resources are automatically released through the "Garbage Collector", but there are still some points to note:

1. the reference of the value type and reference type does not need any "Garbage Collector" to release the memory, because when they are out of scope, the occupied memory will be automatically released, because they are all stored in stacks;

2. only the object instances referenced by reference types are stored in Heap. Because Heap is a free storage space, therefore, it does not have a life period like the "stack" (after the "stack" element pops up, it indicates that the life period ends and the memory is released). Note that, the "Garbage Collector" only applies to this area;

 

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace NET. MST. Third. FinalizeDispose
{
Public class FinalizeDisposeBase: IDisposable
{
// Mark whether the object has been released
Private bool _ disposed = false;

// Finalize method:
~ FinalizeDisposeBase ()
{
Dispose (false );
}

// The Dispose method in IDispose is implemented here.
Public void Dispose ()
{
Dispose (true );

// Tell GC that the Finalize method of this object no longer needs to be called
GC. SuppressFinalize (true );
}

// Perform the actual destructor here
// Declare as a virtual method for the subclass to be rewritten when necessary
Protected virtual void Dispose (bool isDisposing)
{
// When the object has been destructed, it is no longer executed
If (_ disposed)
Return;
If (isDisposing)
{
// Release managed resources here
// Only executed when the user calls the Dispose method
}
// Release unmanaged resources here

// Mark that the object has been released
_ Disposed = true;
}
}

Public sealed class FinalizeDispose: FinalizeDisposeBase
{
Private bool _ mydisposed = false;

Protected override void Dispose (bool isDisposing)
{
// Don't dispose more than once.
If (_ mydisposed)
Return;
If (isDisposing)
{
// Release the hosted resources and declare them in this type.
}
// Release the unmanaged resources stated in this type.

// Call the Dispose method of the parent class to release resources in the parent class
Base. Dispose (isDisposing );

// Set the subclass tag
_ Mydisposed = true;
}

Static void Main ()
{
}
}
}

To release unmanaged resources, you must write code. Usually, you can use the destructor to release the unmanaged resources and place the code snippet that you write to release the unmanaged Resources in the destructor. Note that if an unmanaged resource is not used in a class, do not define the Destructor because the object executes the destructor, therefore, the "Garbage Collector" must call the Destructor before releasing the managed resources, and then release the managed resources for the second time. In this way, the cost of the two Delete actions is more than that of the first action.

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.