Wednesday, August 20, 2014

Disposable Method in C#

Problem:
The memory would not release if you not call the dispose method.

Several Type need to disposable:
  • File handles
  • Network sockets
  • Database connections
  • Unmanaged memory
2 way to dispose object:

1.     Call Dispose method

Need to call dispose method in Try….Finally, because need to dispose after handling the exception. The code will look ugly because have quite Try… Finally method if you have several object need to dispose.

2.     Using

It’s included call dispose method in Try….Finally, so you no need to call few time of Try… Finally, and your code look clean and you would not forget to dispose it.

Conclusion:
You have to do call dispose method in every Idisposable object and Using is more recommended. 

http://coding.abel.nu/2011/12/idisposable-and-using-in-c/

No comments:

Post a Comment