Link to home
Start Free TrialLog in
Avatar of eugene-g
eugene-g

asked on

How to properly dispose of a UserObject

In my C# .NET 2.0 application I have a UserObject "Patient"
During the lifetime of the application this object is being used either to represent a new Patient or to allow making updates to an existing Patient.
In either case I need to make sure that every time this object is used it is completely disposed of.
I have tried to accomplish that by utilizing the "using" keyword within my "SavePatient" method.
As it turns out, this did not do the trick, even though the object itself was gone from the screen.
What is the proper way of accomplishing this?

-Eugene
Avatar of Avodah
Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

When employing the using keyword you UserObject should implement the IDisposable interface which provide the Dispose method that will be called at the end of the using block. The approach is used when unmanaged resources need to be cleaned up, however the UserObject itself will be cleaned up by the Garbage collector. An explicit call to the Garbage Collector GC.Collect() should get rid of reference type allocation on the heap.
ASKER CERTIFIED SOLUTION
Avatar of Avodah
Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial