我需要使用“使用”吗?每个实现 IDisposable 的对象中的关键字?

发布于 2025-01-04 13:34:12 字数 72 浏览 4 评论 0原文

我正在调用一个第三方库,其中有很多类实现了 IDisposable。

我需要对所有这些都使用 using 模式吗?

I am calling a 3rd party library, where so many class implemented IDisposable.

Do I need to use using pattern on all of them?

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

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

发布评论

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

评论(4

风柔一江水 2025-01-11 13:34:13

您并不“需要”这样做,但您很可能应该这样做。

如果您不这样做,您可能会耗尽一些资源,甚至可能会得到不正确的结果,具体取决于该库的具体用途。

但是,如果不知道 Dispose() 对这些对象到底做了什么,您一定应该调用它,以避免出现意外问题。 (您不必直接执行此操作,您可以按照您的建议使用 using 来执行此操作。)

You don't “need” to do it, but you most likely should.

If you don't do that, you can be running out of some resources, or you can even get incorrect results, depending on what exactly does that library do.

But without knowing what exactly does Dispose() do on those objects, you should definitely call it, to avoid unexpected problems. (And you don't have to do that directly, you can use using to do that, as you suggested.)

血之狂魔 2025-01-11 13:34:13

这在很大程度上取决于相关变量的范围。

本地作用域:using

如果变量的作用域是本地的,是的,您应该将相关代码包含在 using 块中。请记住,using 块只是以下内容的语法糖,假设 using 包含一个名为 obj 的 IDisposable 对象:

var obj = // ...
try
{
    // ...
}
finally
{
    obj.Dispose();
}

这意味着即使抛出异常,您的对象将被处置。

类作用域:IDisposable

如果您的对象的作用域是类级别,那么不,您不应该将其包含在 using 块中。相反,您的类应该通过实现 IDisposable 向任何使用该方法的代码公开 Dispose 方法,并在那里处置该对象。

切勿使用:Finalize

通常,通过依赖类的终结器来处置其对象,在此依赖链中的任何点将处置责任转移给垃圾收集器是不好的做法。这破坏了 DisposeFinalize 之间的差异:Dispose 用于显式、立即释放资源,而 Finalize 更重要被动的。通过依赖 Finalize 调用 Dispose,您破坏了这种目的分离。然而,这更多的是我的编程风格问题,代表了一种观点——不要把它当作事实。在接受我的建议之前,你应该自己更多地研究这个问题——并且当然要阅读不可避免的一系列关于此事的评论。我确信我至少错过了重要的例外。

That depends very much on the scope of the variable in question.

Local Scope: using

If the variable is scoped locally, yes, you should enclose relevant code in a using block. Remember, a using block is just syntax sugar for the following, assuming using is enclosing an IDisposable object named obj:

var obj = // ...
try
{
    // ...
}
finally
{
    obj.Dispose();
}

This means that even if an exception is thrown, your object will be disposed.

Class Scope: IDisposable

If your object is scoped at a class level, then no, you should not be enclosing it in a using block. Rather, your class should expose the Dispose method to any code that uses it by implementing IDisposable, and dispose the object there.

Never Use: Finalize

Generally, it is bad practice to transfer disposal responsibility to the garbage collector at any point in this dependency chain by relying on a class's finalizer to dispose its objects. This undermines this difference between Dispose and Finalize: Dispose is for explicit, immediate resource release, while Finalize is more passive. By relying on Finalize to call Dispose, you undermine this separation of purpose. However, this is more a matter of programming style on my part, and represents an opinion--do not take it as a fact. You should research this more on your own--and certainly read the inevitable array of incoming comments on the matter--before taking my advice. I'm sure I missed important exceptions, at the very least.

心不设防 2025-01-11 13:34:13

当一个类实现 IDisposable 时,它​​表示它宁愿由代码处置,也不愿等待垃圾收集器稍后处置它。所以,是的,如果一个类实现了 IDisposable,您应该在它超出范围之前调用 Dispose (或使用 using)。

您应该使用 using 而不是直接调用 Dispose 吗?再说一遍,是的。为了确保对象被处置,即使抛出异常,您也必须确保您的对象被处置,方法是将您的代码封装在 try { ... } finally {} 块中,该块将在 finally 块中处置您的对象。这会导致不必要的混乱,并且您很容易忘记添加finally块。

使用 using 来完成这项工作要安全得多。

When a class implements IDisposable it is saying that it would rather be disposed by your code than wait for the garbage collector to dispose it at a later time. So yes, if a class implements IDisposable you should call Dispose (or use using) before it goes out of scope.

Should you use using instead of calling Dispose directly? Again, yes. To ensure that an object is disposed you would have to make sure your objects were disposed even if an exception was thrown, by encapsulating your code in a try { ... } finally {} block that would dispose your objects in the finally block. This leads to unnecessary clutter and you can easily forget to add the finally block.

It is much safer to use using for this job.

迷鸟归林 2025-01-11 13:34:12

不必这样做,但这是一个很好的做法。

无论是否发生异常,它都能确保正确清理资源。

IDisposable 应该只在需要清理资源的类上实现,因此确保它们这样做是一个很好的做法。

在某些情况下,可能需要直接调用 Dispose 而不是 using 块(WCF 代理因此而臭名昭著),但这不是一般情况。

简而言之 - 没有人会强迫您使用它们,但您确实应该使用它们。

You don't have to, but it is good practice.

It ensures resources are cleaned up properly whether exceptions occur or not.

IDisposable should only be implemented on classes that need to cleanup resources, so ensuring that they do is good practice.

There may be cases that calling Dispose directly instead of a using block will be required (WCF proxies are notorious for this), but this is not the general case.

In short - no one will force you to use them, but you really really should.

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