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

2019-07-25 09:35发布

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();

5条回答
迷人小祖宗
2楼-- · 2019-07-25 10:07

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!

查看更多
We Are One
3楼-- · 2019-07-25 10:09

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

查看更多
forever°为你锁心
4楼-- · 2019-07-25 10:10

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

查看更多
再贱就再见
5楼-- · 2019-07-25 10:10

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.

查看更多
走好不送
6楼-- · 2019-07-25 10:14

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.

查看更多
登录 后发表回答