Dispose in C #, destructor, close to improve distinction of C # program recommendation 4: Dispose implemented in C # Standard Model

Before starting this article, it requires some preparation knowledge. First put forward the "What is a resource." After CLR came out, Windows System Resource start into "unmanaged resources" and "managed resource."

         Unmanaged resources means: all Window kernel objects (handles) are unmanaged resources, such as for Stream, database connections, GDI + related objects, as well as Com objects, etc., these resources are not managed by the CLR;

         Managed Resources means: by the CLR manages the allocation and release of resources, that is new out by the CLR object.

Secondly, again in terms of how resources are released.

         Unmanaged resources: need to explicitly release, you need to write code that is released;

         Managed Resources: do not need to explicitly freed, but if the reference type itself contains unmanaged resources, you need to be realistic release;

 

 

A difference, Close and Dispose of these two methods

After calling the Close method over an object, the object is likely to be re-use; and the Dispose method, the object share some resources need to be labeled as useless, that is, the object to be destroyed and can no longer be used. For example, a common .Net class library SqlConnection class, call the Close method when finished, it can be re-opened through the Open a database connection, when the object is complete without this you can call the Dispose method to mark this object is useless, waiting for GC recovery.

 

Second, the difference between the three of FIG.

 

 

  Destructor Dispose method Close method
significance Destroy the object Destroy the object Close Object Explorer
Called Calls can not be displayed, the recovery is invoked in GC You need to show the call or by using statement It needs to display call
Call timing uncertain Determining, using the display or leave the call block OK, when displaying call

 

Third, the destructor and Dispose Description

  •  Dispose need to implement IDisposable interface.  
  • Dispose called by the code developer, the destructor is automatically invoked by the GC.
  • Dispose method should release all managed and unmanaged resources. Destructor only unmanaged resources should be released. Because destructor from the GC to determine the call, determining if an object is no longer needed GC when its destructor method is called, the object at this time may also contain other useful managed resources.
  • At the end of the Dispose method to tag "GC.SuppressFinalize (this);", i.e., you do not need to tell the GC method calls the destructor of the object, otherwise, the determination will GC its destructor method after the object is no longer useful, Although the program can not go wrong, but the impact system performance.
  • Dispose destructor releases resources and should be the same, so that even in the absence of a user class calls Dispose, the resource will be released in the Finalize.
  • Finalize should not be public.
  • Released by frequently calling the destructor method GC system resources can degrade system performance, it is recommended to display call the Dispose method.
  • Dispose method when there is, it should be called, because Finalize to release resources are usually very slow.

 

Stating Close function

 Close this method has different meanings in different classes, and there is no requirement Close has a special meaning, that Close does not have to release resources, you can also let the Close method represents a "closed." However, due to Close the "off" means, often used to free up resources to Close, which is also allowed. For example, file operations, with the release of Close objects appear to be more accurate than Dispose meaning, so when designing a class, you can Close to public, set the Dispose protected, then call the Dispose Close. Close What does it mean to say that it will not release resources, completely determined by the class designer. Online said "Close call Dispose" This method is very one-sided. In the Close SqlConnection just means close the database connection, and did not release the SqlConnection object resources. According to the case experience, Close and Dispose exist simultaneously (both public), Close does not mean that the release of resources, because usually, the class designer should not use the same method to release two public resources.

V. destructor and Dispose method of Example 

Copy the code
Public class BaseResource 1: IDisposable 
 2 ... { 
 3 ~ BaseResource () 
 4 ... { 
 5 // In order to maintain code readability and maintainability, do not be here to write the code to release unmanaged resources 
 6 // must be the Dispose (false) mode called to tell the false Dispose (bool disposing) function is called from the garbage collector when calling the destructor 
 . 7 the Dispose (false); 
 . 8} 
 . 9 // can not directly call the client 
10 // If disposing is true, then this method is called directly the customer, hosting, and unmanaged resources can release 
11 // If disposing is false, then the function is called from the garbage collector when calling the Finalize at this time should not reference other managed objects so that only release unmanaged resources 
12 protected Virtual void the Dispose (BOOL the disposing) 
13 ... { 
14 // then this method is called directly in the client, then the hosting, and non managed resources may be released 
15 IF (the disposing) 
16 ... { 
. 17 releases the managed resources // 
18 OtherManagedObject.Dispose ();
}. 19 
20 // release unmanaged resources
DoUnManagedObjectDispose 21 (); 
22 // then this method is called directly in the client, tell the garbage collector to clear himself from the Finalization queue, thereby preventing the garbage collector calls the destructor method. 
23 IF (the disposing) 
24-GC.SuppressFinalize ( the this); 
25} 
26 // can be called directly customers 
27 public void the Dispose () 
28 ... { 
29 // must call to Dispose (true) way to tell the true Dispose (bool disposing) function is called directly customers of 
30 the Dispose (to true); 
31 is} 
32}
Copy the code


reference:

Managed resource: the CLR manages the allocation and release of resources, that is new out by the CLR objects;

Unmanaged resources: not managed by the CLR objects, windows kernel objects, such as files, database connections, sockets, COM objects, and so on;

Recommendations to improve the program C # 4: Implementing the C # standard Dispose pattern

Guess you like

Origin www.cnblogs.com/nafio/p/10931188.html