Exploring the differences between the Dispose method and the Close method in C #

Source: Internet
Author: User

Someone in the group asked how to directly clear the string value in the heap. Someone suggested using the Dispose () method directly; Dispose () destroys the object, which is a garbage collection mechanism. (Using may be better here)
When developing C # code, we often encounter a problem. Some classes provide Close () and some classes provide Dispose (). What is the difference between Dispose and Close?

Here, we need to clarify the resources in the C # Program (or. NET. In short, every type in C # represents a resource, whileThere are two types of resources:
Managed resources:
Resources allocated and released by the CLR are managed, that is, new objects in the CLR;
Unmanaged resources:Objects not managed by CLR, windows kernel objects, such as files, database connections, sockets, and COM objects. Without exception, if our types Use unmanaged resources, or you need to explicitly release the managed resources, you need to make the type inheritance interface IDisposable. This is equivalent to telling the caller that this type requires explicit resource release and you need to call my Dispose method.

First, Dispose and Close should be basically the same. Close is designed for developers who are not familiar with Dispose. Because basically all developers know what Close is (especially for developers with a C ++ background ).
But when writing code, if you want to implement Close and Dispose, pay attention to the design mode of Close and Dispose .. Net class only provides Close, and derived fromIDisposableAnd hiddenDispose Method. Do you think it is hard to understand?

For these classes, the key lies in their explicit implementation of IDisposable. For implicit implementation, you only need to call "newA (). Dispose ()", but for explicit implementation, Dispose is not a member function of this class. The only call method is that you must first cast to IDisposable. ("New A (). Dispose ()" cannot be compiled, but "(IDisposable) newA (). Dispose ()" can be compiled ). So it meets the design requirements:Provide Close (), hide Dispose (), and implement the IDisposable interface.

In. net framework, Close () is designed to be public, and call the hidden Dispose () in Close (); Dispose () to call another virtual Dispose (bool) function. Therefore, if you inherit from this class, you must implement the Dispose (bool) method.

When you call Close (), you will call the overloaded Dispose (bool) method to release the resource.
The following is a standard implementation method that inherits the IDisposable interface type. This implementation is called the Dispose mode:

Copy codeThe Code is as follows: public class SampleClass: IDisposable
{
// Demonstrate creating an unmanaged Resource
Private IntPtr nativeResource = Marshal. AllocHGlobal (100 );
// Demonstrate creating a Managed Resource
Private AnotherResource managedResource = new AnotherResource ();
Private bool disposed = false;
/// <Summary>
/// Implement the Dispose method in IDisposable
/// </Summary>
Public void Dispose ()
{
// The value must be true.
Dispose (true );
// Notify the garbage collection mechanism not to call the terminator (destructor)
GC. SuppressFinalize (this );
}
/// <Summary>
/// It is not necessary to provide a Close method only to better comply with the specifications of other languages (such as C ++ ).
/// </Summary>
Public void Close ()
{
Dispose ();
}
/// <Summary>
/// Required, in case that the programmer forgets to explicitly call the Dispose method
/// </Summary>
~ SampleClass ()
{
// The value must be false.
Dispose (false );
}
/// <Summary>
/// Use protected virtual for non-seal class Modification
/// Private for sealing class Modification
/// </Summary>
/// <Param name = "disposing"> </param>
Protected virtual void Dispose (bool disposing)
{
If (disposed)
{
Return;
}
If (disposing)
{
// Clear managed resources
If (managedResource! = Null)
{
ManagedResource. Dispose ();
ManagedResource = null;
}
}
// Clear unmanaged Resources
If (nativeResource! = IntPtr. Zero)
{
Marshal. FreeHGlobal (nativeResource );
NativeResource = IntPtr. Zero;
}
// Let the type know that it has been released
Disposed = true;
}
Public void SamplePublicMethod ()
{
If (disposed)
{
Throw new ObjectDisposedException ("SampleClass", "SampleClass is disposed ");
}
// Omitted
}
}

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.