Note that there are some explanatory texts on larger screens.

plurals
  1. POFinalize/Dispose pattern in C#
    text
    copied!<p>C# 2008</p> <p>I have been working on this for a while now, and I am still confused about some issues. My questions are below</p> <ol> <li><p>I know that you only need a finalizer if you are disposing of unmanaged resources. However, if you are using managed resources that make calls to unmanaged resources, would you still need to implement a finalizer?</p></li> <li><p>However, if you develop a class that doesn't use any unmanaged resources, directly or indirectly, can you implement the <code>IDisposable</code> so that clients of your class can use the 'using statement'? </p> <p>Would it be acceptable to implement the IDisposable just so that clients of your class can use the using statement?</p> <pre><code>using(myClass objClass = new myClass()) { // Do stuff here } </code></pre></li> <li><p>I have developed this simple code below to demonstrate the Finalize/dispose pattern:</p> <pre><code>public class NoGateway : IDisposable { private WebClient wc = null; public NoGateway() { wc = new WebClient(); wc.DownloadStringCompleted += wc_DownloadStringCompleted; } // Start the Async call to find if NoGateway is true or false public void NoGatewayStatus() { // Start the Async's download // Do other work here wc.DownloadStringAsync(new Uri(www.xxxx.xxx)); } private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { // Do work here } // Dispose of the NoGateway object public void Dispose() { wc.DownloadStringCompleted -= wc_DownloadStringCompleted; wc.Dispose(); GC.SuppressFinalize(this); } } </code></pre></li> </ol> <p>Question about the source code:</p> <ol> <li><p>Here I have not added the finalizer, and normally the finalizer will be called by the GC, and the finalizer will call the Dispose. As I don't have the finalizer, when do I call the Dispose method? Is it the client of the class that has to call it?</p> <p>So my class in the example is called NoGateway and the client could use and dispose of the class like this: </p> <pre><code>using(NoGateway objNoGateway = new NoGateway()) { // Do stuff here } </code></pre> <p>Would the Dispose method be automatically called when execution reaches the end of the using block, or does the client have to manually call the dispose method? i.e.</p> <pre><code>NoGateway objNoGateway = new NoGateway(); // Do stuff with object objNoGateway.Dispose(); // finished with it </code></pre></li> <li><p>I am using the webclient class in my <code>NoGateway</code> class. Because the webclient implements the IDisposable interface, does this mean that the webclient indirectly uses unmanaged resources? Is there a hard and fast rule to follow about this? How do I know that a class uses unmanaged resources?</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload