c# - still trying to understand the dispose pattern -
I have read MSDN and various positions about settling the pattern, and there are still some things that I do not understand . I have written the following code to test the settlement pattern Please note that there are not extraordinary resources, I am using versus 2008 and 3.5.
Public Partial Category Form 1: Form {Public Form 1 () {Initial Timeliness}; } Try Private Zero () (// Test 1: Assign the resource and assign it to GC BL.MEXA class DF = new BL.Maxal (); // test2: Try to collect garbage / GC / compiler () ; // Test 3: Dismiss the call itself // // (BL.myclass df = new BL.myclass ()) // {}} Private Zero Button 1_Click (Object Sender, EventArgs) {tryDispose () ;}
This is my disposable category:
class myclass: IDisposable {private StronglyTypedDs myDS; Private Bull _ Dispute; public myclass () {(myDSTableAdapter DocDo Cadpt = new myDSTableAdapter ()) {myDS = new StronglyTypedDs (); docDocadpt.Fill (myDS.TheTable);}} # Area IDisposable Members Public Disposal Disposal () (Dispose (True); GC.SuppressFinalize (this);} ~ Myclass (Removal);} Protected Virtual Wide Dispos (Bull Displenence) {if (! _disposed) {if (disposing) {if (myDS! = Null) myDS .design (); MyDS = null;}} _disposed The results are: Result:
Test 1A - instantiating just myclass, destructor is comment because myclass does not contain unmanaged resources: myclass. Is not said to be dispose, even if A. Application'm closes (which settlement is executed instead). So once I close the application, what is the status of the dataset?
Test 1B - Controlling Destructor, it is fine that when I close the application, then all disposers are called.
Test 2A and 2B - I have just given the above test to GC. 1A and 1B is similar to testing
Test 3 - Everything is fine (absolutely)
Many posts say, if there is no unmanaged resource in Micheles , So I do not need to add the dishes; So far in my tests, if I do not connect the destructor then myclass.dispose is called when I do not close the application. I have not waited for GC to run by myself (if I remember correctly that gc.collect does not guarantee class installation then) to check whether it will call myclass.dispose. So what is the correct implementation: Always define the E districtist or to avoid it if there are only managed resources in my class? If I had filled the levels of all generations, would there be a class that would not have settled or classified my class without applying a district?
In the end, I have seen that if I define a destroyer, but do not declare class as implementing idispable, then the settlement series works in any way. This can be understood because the destroyer can be translated to finalize in IIL. But I find it really confusing: is it any "inherent" interface implementation that I do not know? GC can settle items but users can not thank you in advance
Stephano "Itemprop =" text ">
Trust your garbage collector. Managed resources will be cleaned up, eventually.
It generally means that you should only implement when you either:
Indigenous resources are wrapping (in this case, you probably also want a final form.) - Encapsulating a class that is applied to
IDisposable
. (In this case, you want IDisposable
, but there is no need to apply any final / destructor.)
IDisposable
In my opinion, it covers a lot of usage cases, and depending on what you are doing, proper implementation changes (ie: wrapping up the original resources, wrapping up other IDisposable classes or using factored types ). To solve this, I have written - it can help to clarify some of these for you.
Comments
Post a Comment