code_junkie

How do you manage deterministic finalization in C#?

Nov 11th, 2011
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. class MyDisposableObject : IDisposable
  2. {
  3. public MyDisposableObject()
  4. {
  5. }
  6.  
  7. ~MyDisposableObject()
  8. {
  9. Dispose(false);
  10. }
  11.  
  12. private bool disposed;
  13. private void Dispose(bool disposing)
  14. {
  15. if (!this.disposed)
  16. {
  17. if (disposing)
  18. {
  19. // Dispose of your managed resources here.
  20. }
  21.  
  22. // Dispose of your unmanaged resources here.
  23.  
  24. this.disposed = true;
  25. }
  26. }
  27.  
  28. void IDisposable.Dispose()
  29. {
  30. Dispose(true);
  31. GC.SuppressFinalize(this);
  32. }
  33. }
  34.  
  35. public void DoingMyThing()
  36. {
  37. using (MyDisposableObject obj = new MyDisposableObject())
  38. {
  39. // Use obj here.
  40. }
  41. }
Add Comment
Please, Sign In to add comment