问题
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