Wednesday, September 21, 2022

C# - Dispose Rule

Tips

  1. All IDisposable objects must be disposed. There are only few exceptions e.g. https://stackoverflow.com/questions/38784434/how-to-properly-dispose-the-stream-when-using-streamcontent
  2. Use "using" by default for all IDisposable classes. If "using" cannot be used, at least "try ... finally" must be used and dispose inside "finally".
  3. If a method returns an IDisposable classes, it is the responsibility of the caller to dispose it properly.
  4. Try to keep IDisposable objects lifetime as short as possible e.g. Bitmap

No comments:

Post a Comment