Which three actions should you perform?

You are creating a class that uses unmanaged resources.
This class maintains references to managed resources on other objects.
You need to ensure that users of this class can explicitly release resources when the class instance ceases to be needed.
Which three actions should you perform?
(Each correct answer presents part of the solution. Choose three.)

You are creating a class that uses unmanaged resources.
This class maintains references to managed resources on other objects.
You need to ensure that users of this class can explicitly release resources when the class instance ceases to be needed.
Which three actions should you perform?
(Each correct answer presents part of the solution. Choose three.)

A.
Define the class such that it inherits from the WeakReference class.

B.
Define the class such that it implements the IDisposable interface.

C.
Create a class destructor that calls methods on other objects to release the managed resources.

D.
Create a class destructor that releases the unmanaged resources.

E.
Create a Dispose method that calls System.GC.Collect to force garbage collection.

F.
Create a Dispose method that releases unmanaged resources and calls methods on other objects to release the managed resources.

Explanation:

It is necessary to implement the IDisposable interface if you need to release unmanaged resources or
want explicit control of the life of managed resources. A class destructor should be created to release the unmanaged resources and this should be called from within the Dispose method. The dispose method should also release the managed resources.

Inheriting from WeakReference would result in the garbage collector releasing resources even though there may be valid references.

The managed resources should be released in the Dispose method.

System.GC.Collect could be used, however it is more efficient to manually release the managed resources.
The GC incurs overhead and may have only recently been called anyway. The question states resources should be released explicitly.



Leave a Reply 1

Your email address will not be published. Required fields are marked *


mr_tienvu

mr_tienvu

I agree with the answer.