处理掉这个对象就足够了吗? 或者我需要做更多的事情吗?

发布于 2024-07-10 06:16:32 字数 238 浏览 2 评论 0原文

我有这段代码:-

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

我是否需要在离开该处置范围之前放置这行代码..或者已经进行处置“调用”。

sha2.Clear();

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

樱娆 2024-07-17 06:16:32

由于据我所知,Clear() 方法仅调用 Dispose,因此 using 块应该足以确保释放所使用的资源。

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

心房敞 2024-07-17 06:16:32

恕我直言,如果调用 Dispose() 不足以处置对象,那么要么代码中存在严重错误,要么设计中存在严重缺陷。 因此,不必担心在您自己的代码中采取任何其他步骤!

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!

如此安好 2024-07-17 06:16:32

如果您使用 Reflector 看一下,您会发现 Clear 仅调用 Dispose,因此无需在示例中调用 Clear

许多框架类为 Dispose 提供了 Close/Clear/Whatever 覆盖,以使使用更加简单。

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.

漫雪独思 2024-07-17 06:16:32

还有一个通用的有用提示 - 不要忘记现在所有这些东西的来源都是可用的 - 它经常帮助我回答这类问题,而不必猜测或推断。

这是一个很好的起点:http://www.codeplex.com/NetMassDownloader

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

久而酒知 2024-07-17 06:16:32

Dispose() 已经足够好了。

我不确定 .NET 是如何工作的。 但是附加函数调用或“set null”会降低Java的性能。

CLR/Java VM 将(并且必须)能够在下一次垃圾收集中从“根”清除所有取消引用的托管对象。

附言。
Dispose() 会清理“非托管”资源,以提高 GC 性能,因为它不等待 Finalllizer 线程完成。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文