It is recommended that the 48:dispose method be allowed to be called multiple times
A type of Dispose method should be allowed to be called multiple times without throwing an exception. In view of this, the type internally maintains a private bool variable disposed, as follows:
Private BOOL false;
In the actual method of cleaning the code, add a judgment:
if (disposed) { return; } // omit the code for the Cleanup section and assign a value of disposed to true at the end of the method true;
This means that if the type has been cleaned up, the cleanup will no longer work.
object is called by the Dispose method and does not indicate that the object is set to NULL, and that the garbage collection mechanism reclaims the memory and has been completely stored. In fact, the reference to the object may still be in. However, if the object is dispose of, the normal state of the object is no longer present, and if the object's public method is called, a objectdisposedexception should be thrown. Method Samplepublicmethod demonstrates this method for us:
Public void Samplepublicmethod () { if (disposed) { thrownew objectdisposedexception ( " SampleClass " " SampleClass is disposed " ); } // omitted }
Therefore, in Dispose mode, you should always create a variable for the type that indicates whether the object has been dispose of.
Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia
157 recommendations for writing high-quality code to improve C # programs--suggest that the 48:dispose method should allow multiple calls