Is disposing this object, enough? or do i need to do more?

社会主义新天地 提交于 2020-01-16 02:27:25

问题


I have this code :-

using (System.Security.Cryptography.SHA256 sha2 = 
    new System.Security.Cryptography.SHA256Managed())
{ .. }

Do I need to put this line of code, just BEFORE I leave that dispose scope .. or does the dispose 'call' that already.

sha2.Clear();

回答1:


Since AFAIK the Clear() method just calls Dispose, the using block should be enough to ensure that the resources used are released.




回答2:


IMHO if calling Dispose() is not enough to dispose off an object then either there is a serious bug in the code or a serious flaw in the design. So don't worry about taking any additional steps in your own code!




回答3:


If you take a look using Reflector, you'll see that Clear just calls Dispose, so there's no need to call Clear in your example.

Many of the framework classes offer a Close/Clear/Whatever cover for Dispose to make the usage a little more straightforward.




回答4:


And a generic helpful hint - don't forget at that the source for all this stuff is available nowadays - it often helps me answer this sort of question, without having to guess or infer.

This is a good place to start: http://www.codeplex.com/NetMassDownloader




回答5:


Dispose() is good enough.

I am not sure how .NET works. But addition function call or "set null" will degrade the performance in Java.

The CLR/Java VM will(and must) able to cleanup all dereferenced managed object from "roots" in the next garbage collection.

PS. Dispose() does cleanup "unmanaged" resources, to improve the GC performance as it don't wait for the Finallizer thread to complete.



来源:https://stackoverflow.com/questions/328743/is-disposing-this-object-enough-or-do-i-need-to-do-more

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!