Complete Disposable statement

Source: Internet
Author: User

Public class BaseDisposable: IDisposable
{
~ BaseDisposable ()
{
// The Garbage Collector calls this method, so the parameter must be false.
Dispose (false );
}

/// <Summary>
/// Whether the Dispose (bool disposing) method has been called.
/// Should be defined as private, so that the base class and subclass do not affect each other.
/// </Summary>
Private bool disposed = false;

/// <Summary>
/// This method is used to complete all recycling tasks.
/// Subclass should overwrite this method.
/// </Summary>
/// <Param name = "disposing"> </param>
Protected virtual void Dispose (bool disposing)
{
// Avoid calling Dispose repeatedly.
If (! Disposed) return;

// Adapt to the multi-threaded environment to avoid thread errors.
Lock (this)
{
If (disposing)
{
//------------------------------------------------
// Write the code for releasing managed resources here
// (1) if the Dispose () method exists, call its Dispose () method.
// (2) if there is no Dispose () method, set it to null.
// Example:
// XxDataTable. Dispose ();
// XxDataAdapter. Dispose ();
// XxString = null;
//------------------------------------------------
}

//------------------------------------------------
// Write and release unmanaged resources here
// Example:
// File handle
//------------------------------------------------
Disposed = true;
}
}

/// <Summary>
/// This method is called by a program. After this method is called, the object is terminated.
/// This method is defined in the IDisposable interface.
/// </Summary>
Public void Dispose ()
{
// Because the program calls this method, the parameter is true.
Dispose (true );
// Because we do not want the Garbage Collector to terminate the object again, we need to remove the object from the end list.
GC. SuppressFinalize (this );
}

/// <Summary>
/// Call the Dispose () method to recycle resources.
/// </Summary>
Public void Close ()
{
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.