title: Is it a good idea to have a class member that holds a resource for the whole object lifetime?
body: <p>I could have a class that has a member that holds a resource or at least it implements <code>IDisposable</code>. Here is one example (just for showing what I mean):</p>
  
  <pre><code>public class Something
  {
      private HMACSHA1 HmacSha1;
  
      public Something()
      {
          HmacSha1 = new HMACSHA1();
      }
  
      public byte[] DoIt(byte[] bytes)
      {
          return HmacSha1.ComputeHash(bytes);
      }
  }
  </code></pre>
  
  <p>The member <code>HmacSha1</code> implements <code>IDisposable</code> and should therefore be disposed at the end of the object's lifetime. I know that and it's not part of my question and intentionally left out in the example. Also it could be of any other type holding a resource, like a database or network connection.</p>
  
  <p>I want to know whether it is a good idea to keep that member <em>open</em> (I mean created once in the beginning and not <em>disposed</em>) for the whole lifetime of the object?</p>
  
  <p>Or would it be better to create and dispose it where it is needed - in method <code>DoIt()</code> in the above example?</p>
  
  <p>What are the downsides?</p>