Dispose x Close

Luciano

In overall cases, the Close method semantically just change the state of a object that can be changed again with the Open method, indefinitely.

In other hand the semantics of IDisposable.Dispose() method put the object in a state that cannot be undone.

So, why several answers about "close x dispose" and even MSDN pattern shows Close method calling Dispose ?

Hans Passant

There is plenty of friction about this is the .NET framework itself. The implementation is however consistently without surprise, a class' Close() method always calls its Dispose() method.

Logically it always means the exact same thing. You explicitly say "I don't use this object anymore" in your code. So of course Close() just means the exact same thing as Dispose(). They both mean the exact same thing, "I'm done using it". So of course the implementation of these methods do the exact same thing.

Semantically it however does not mean the same thing. The big dog is the using statement.

You, preferably, never call Dispose() explicitly. The only place is should ever appear in your code is in your own IDisposable implementation. Which you need to implement when your class has members that implement IDisposable themselves. Which obliges you to implement IDisposable yourself, you simply implement it by calling the members' Dispose method.

Which is the only place you ever use it. In any other usage, you leave it up to the using statement inside of a method to automatically call Dispose() for you. With the corner-case that you don't want to dispose right away, you may need to keep the object around beyond the method call. That's when you use the Close() method.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Close/Dispose on FileStream

From Dev

Reasons to use Close over Dispose for a Stream?

From Dev

Difference between dispose and exit on close in java

From Dev

How to Dispose resources when the WPF form close

From Dev

dispose() vs setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

From Dev

JxBrowser HEAVYWEIGHT Dispose on WINDOW_CLOSE_REQUEST

From Dev

How to Dispose resources when the WPF form close

From Dev

dispose() vs setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

From Dev

How to close or dispose of TCP client properly?

From Dev

How to properly close ODP.net connection : dispose() or close()?

From Dev

Is it safe to call .Close (.Dispose) on an EventWaitHandle directly after .Set?

From Dev

What are the differences between Process.Close() and Process.Dispose()?

From Dev

Proper way to close/dispose of a ServiceHost Thread in C#?

From Dev

unable to stop stream when i DISPOSE_ON_CLOSE

From Dev

How to make windows form release/close/dispose image file

From Dev

Unable to Close/Dispose a File lock on SQLite database C#

From Dev

unable to stop stream when i DISPOSE_ON_CLOSE

From Dev

Do I actually need to call FileStream.Close/Dispose

From Dev

frm.showDialog dispose when openFileDialog close [vb.net]

From Dev

How to dispose JOptionPane after x seconds

From Dev

How to dispose JOptionPane after x seconds

From Dev

Method not found X509Chain.Dispose()

From Dev

Close button X not removed

From Dev

Close menu on X click

From Dev

Close button X not removed

From Dev

Unable to close the Frame by clicking close X button

From Dev

Vaadin: Close Notofications by "close", "ok", or "x"?

From Dev

Unable to close the Frame by clicking close X button

From Dev

Close materialize sidenav with 'X' Icon

Related Related

  1. 1

    Close/Dispose on FileStream

  2. 2

    Reasons to use Close over Dispose for a Stream?

  3. 3

    Difference between dispose and exit on close in java

  4. 4

    How to Dispose resources when the WPF form close

  5. 5

    dispose() vs setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  6. 6

    JxBrowser HEAVYWEIGHT Dispose on WINDOW_CLOSE_REQUEST

  7. 7

    How to Dispose resources when the WPF form close

  8. 8

    dispose() vs setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  9. 9

    How to close or dispose of TCP client properly?

  10. 10

    How to properly close ODP.net connection : dispose() or close()?

  11. 11

    Is it safe to call .Close (.Dispose) on an EventWaitHandle directly after .Set?

  12. 12

    What are the differences between Process.Close() and Process.Dispose()?

  13. 13

    Proper way to close/dispose of a ServiceHost Thread in C#?

  14. 14

    unable to stop stream when i DISPOSE_ON_CLOSE

  15. 15

    How to make windows form release/close/dispose image file

  16. 16

    Unable to Close/Dispose a File lock on SQLite database C#

  17. 17

    unable to stop stream when i DISPOSE_ON_CLOSE

  18. 18

    Do I actually need to call FileStream.Close/Dispose

  19. 19

    frm.showDialog dispose when openFileDialog close [vb.net]

  20. 20

    How to dispose JOptionPane after x seconds

  21. 21

    How to dispose JOptionPane after x seconds

  22. 22

    Method not found X509Chain.Dispose()

  23. 23

    Close button X not removed

  24. 24

    Close menu on X click

  25. 25

    Close button X not removed

  26. 26

    Unable to close the Frame by clicking close X button

  27. 27

    Vaadin: Close Notofications by "close", "ok", or "x"?

  28. 28

    Unable to close the Frame by clicking close X button

  29. 29

    Close materialize sidenav with 'X' Icon

HotTag

Archive