Implementation example of the idisposable Interface

Source: Internet
Author: User
The concept of hosting emerged after the birth of the. NET Framework. In plain terms, it runs under the. NET Framework and is hosted by applications managed by the. NET Framework or other components, and vice versa.

That is to say, it was developed on the. NET platform.ProgramIt should be hosted, and all programs developed before. NET are not hosted. However, unmanaged programs can be re-generated on the. NET platform and become hosted.

. The. NET platform provides GC (garbage collection) for Memory Management, which is responsible for automatically releasing managed resources and memory recovery. However, it cannot release unmanaged resources, at this time, we must provide our own methods to release the unmanaged resources allocated in the object. For example, in the implementation of the objectCodeA com object is used.

The simplest way is to release unmanaged resources by implementing protected void finalize (), because GC checks whether the finalize () method is implemented when releasing an object, if yes, call it. But this will reduce the efficiency ......

There is a better way, that is, to explicitly provide an interface to the client call end to manually release objects, instead of waiting for GC to release our objects (not to mention the low efficiency ).

There is an idisposable interface in the system namespace. It is very suitable to do this, so we need to declare another interface on our own.

In addition, this implementation does not need to be used only after hosting resources are used. If the class you design has a larger instance (such as geometry in GIS) at runtime, in order to optimize program performance, you can also implement this interface to allow the client call end to manually release these objects when they are confirmed that they are not needed.

Sample Code

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace Disposabledemo
{
Class Program
{
Class class1: idisposable
{
// Destructor. After compilation, it becomes protected void finalize (). GC will call this method before reclaiming the object.
~ Class1 ()
{
Dispose (false );
}

// By implementing this interface, the customer can release objects explicitly without waiting for GC to release resources. It is said that this will reduce the efficiency.
Void dispose ()
{
Dispose (true );
}

// Design the release of unmanaged resources into a virtual function to provide the ability to release the base class resources in the inheritance class
Protected virtual void releaseunmanageresources ()
{
// Do something...
}

// Private functions are used to release unmanaged resources.

Private void dispose (bool disposing)
{
Releaseunmanageresources ();

// If the value is true, it indicates that the release function is explicitly called by the customer. You must notify GC not to call the Finalize method of the object.

// If it is false, the GC must have called the Finalize method of the object, so there is no need to tell GC that you do not want to call my Finalize method.

If (disposing)
{
GC. suppressfinalize (this );
}
}
}

Static void main (string [] ARGs)
{
// Tmpobj1 does not manually release the resource, so it will be slowly released by GC.

Class1 tmpobj1 = new class1 ();

// Tmpobj2 calls the dispose method, which is more efficient than waiting for GC to release it.

Class1 tmpobj2 = new class1 ();

(Idisposable) tmpobj2). Dispose ();
}
}
}

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.