IDisposable.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. /*============================================================
  5. **
  6. ** Interface: IDisposable
  7. **
  8. **
  9. ** Purpose: Interface for assisting with deterministic finalization.
  10. **
  11. **
  12. ===========================================================*/
  13. namespace System
  14. {
  15. // IDisposable is an attempt at helping to solve problems with deterministic
  16. // finalization. The GC of course doesn't leave any way to deterministically
  17. // know when a finalizer will run. This forces classes that hold onto OS
  18. // resources or some sort of important state (such as a FileStream or a
  19. // network connection) to provide a Close or Dispose method so users can
  20. // run clean up code deterministically. We have formalized this into an
  21. // interface with one method. Classes may privately implement IDisposable and
  22. // provide a Close method instead, if that name is by far the expected name
  23. // for objects in that domain (ie, you don't Dispose of a FileStream, you Close
  24. // it).
  25. //
  26. // This interface could be theoretically used as a marker by a compiler to
  27. // ensure a disposable object has been cleaned up along all code paths if it's
  28. // been allocated in that method, though in practice any compiler that
  29. // draconian may tick off any number of people. Perhaps an external tool (like
  30. // like Purify or BoundsChecker) could do this. Instead, C# has added a using
  31. // clause, which will generate a try/finally statement where the resource
  32. // passed into the using clause will always have it's Dispose method called.
  33. // Syntax is using(FileStream fs = ...) { .. };
  34. //
  35. // Dispose should meet the following conditions:
  36. // 1) Be safely callable multiple times
  37. // 2) Release any resources associated with the instance
  38. // 3) Call the base class's Dispose method, if necessary
  39. // 4) Suppress finalization of this class to help the GC by reducing the
  40. // number of objects on the finalization queue.
  41. // 5) Dispose shouldn't generally throw exceptions, except for very serious
  42. // errors that are particularly unexpected. (ie, OutOfMemoryException)
  43. // Ideally, nothing should go wrong with your object by calling Dispose.
  44. //
  45. // If possible, a class should define a finalizer that calls Dispose.
  46. // However, in many situations, this is impractical. For instance, take the
  47. // classic example of a Stream and a StreamWriter (which has an internal
  48. // buffer of data to write to the Stream). If both objects are collected
  49. // before Close or Dispose has been called on either, then the GC may run the
  50. // finalizer for the Stream first, before the StreamWriter. At that point, any
  51. // data buffered by the StreamWriter cannot be written to the Stream. In this
  52. // case, it doesn't make much sense to provide a finalizer on the StreamWriter
  53. // since you cannot solve this problem correctly.
  54. public interface IDisposable
  55. {
  56. void Dispose();
  57. }
  58. }