title: Use of Finalize/Dispose method in C#
body: <p>C# 2008</p>
  
  <p>I have been working on this for a while now, and I am still confused about the use of finalize and dispose methods in code. My questions are below:</p>
  
  <ol>
  <li><p>I know that we only need a finalizer while disposing unmanaged resources. However, if there are managed resources that make calls to unmanaged resources, would it still need to implement a finalizer?</p></li>
  <li><p>However, if I develop a class that doesn't use any unmanaged resource -  directly or indirectly, should I implement the <code>IDisposable</code> to allow the clients of that class to use the 'using statement'? </p>
  
  <p>Would it be feasible to implement IDisposable just to enable clients of your class to 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 use:</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 <code>WebClient</code> class in my <code>NoGateway</code> class. Because <code>WebClient</code> implements the <code>IDisposable</code> interface, does this mean that <code>WebClient</code> indirectly uses unmanaged resources? Is there a hard and fast rule to follow this? How do I know that a class uses unmanaged resources?</p></li>
  </ol>