在每个函数中使用语句 ->通过适当的清理转换为类字段?
基本上我有一些如下所示的函数:
class MyClass
{
void foo()
{
using (SomeHelper helper = CreateHelper())
{
// Do some stuff with the helper
}
}
void bar()
{
using (SomeHelper helper = CreateHelper())
{
// Do some stuff with the helper
}
}
}
假设我可以在每个函数中使用相同的资源而不是不同的资源[实例],在清理等方面可以这样做吗?:
class MyClass
{
SomeHelper helper = CreateHelper();
// ...foo and bar that now just use the class helper....
~MyClass()
{
helper.Dispose();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,不要添加析构函数(终结器)。
您可以重用该资源,但您的类必须实现
IDisposable
。现在您必须在 using 块中使用
MyClass
实例。它本身已成为托管资源。析构函数是没有用的,每当收集 MyClass 实例时,关联的帮助器对象也将位于同一个集合中。但拥有析构函数仍然会产生相当大的开销。
IDisposable 的标准模式使用
virtual void Dispose (bool disposeing)
方法,但在使类sealed
时,您可以使用上面的简约实现。No, do not add a destructor (Finalizer).
You can reuse the resource but then your class has to implement
IDisposable
.And now you have to use
MyClass
instances in a using block. It self has become a managed resource .A destructor is of no use, whenever a MyClass instance is being collected the associated helper object will also be in the same collection. But having a destructor still incurs considerable overhead.
The standard pattern for IDisposable uses a
virtual void Dispose(bool disposing)
method but when making the classsealed
you can use the minimalistic implementation above.在.NET 你不知道终结器何时(或是否)根本没有调用。
相反,明确表明您的类将被 ="nofollow">实现
IDisposable
:(这正是
SomeHelper
所做的)我使用
readonly
来确保helper
不会在类中的其他地方重新分配,但是这个如果你小心的话,真的没关系。您必须格外小心,切勿将其设置为
null
,否则您的Dispose
将引发异常。如果该字段受保护
,您可以在对其调用Dispose
之前检查是否为空,这样您就知道自己是安全的。In .NET you don't know when (or whether) finalizer is called at all.
Instead, explicitly indicate that your class is to be disposed of by implementing
IDisposable
:(This is exactly what
SomeHelper
does)I used
readonly
to ensurehelper
doesn't get re-assigned somewhere else in the class, but this really doesn't matter if you're careful.You must be extra careful to never set it to
null
, or yourDispose
will throw an exception. If the field isprotected
, you can check for nullity before callingDispose
on it so you know you're playing safe.您可以在对象的生命周期内共享此类资源,在这种情况下,建议您实现IDisposable。
You can share such a resource during the lifetime of your object, in which case it is recommended that you implement IDisposable.
不,事实并非如此。你不知道什么时候最终确定。此外,如果您的资源受到管理,它将在某个时候被处置,而不会最终确定。
如果您不想一直使用using,也许您可以在许多函数中使用一次。
No, it is not. You don't know when the finalized will un. Also, if your resource is managed, it will be disposed of at some point without the finalized.
If you don't want to use using all the time, perhaps ou can use it once around many functions.
约定是,如果您的类拥有一个
IDisposable
对象,它也应该实现IDisposable
。因此,您的类不应该实现终结器,而应该实现 IDisposable 并在那里处理助手。实现终结器的一个问题是您无法控制它何时被调用。一次性模式为您提供了一种更具确定性的资源清理方式。
The convention is that if your class owns an
IDisposable
object it should also implementIDisposable
. So rather than implementing a finalizer your class should implementIDisposable
and dipose of the helper there.One problem with implementing the finalizer is that you have no control over when it's being called. The disposable pattern gives you a more deterministic way of cleaning up resources.
您不需要重写对象中的终结器,您已在第二个代码示例中通过
~MyClass()
显示了该终结器。您需要实现
IDisposable
模式。如果您使用托管和非托管资源,您的问题并没有明确,但这里有一个托管资源的快速示例。 Stackoverflow 有无数这方面的例子。 Reed Copsey 也有一个很好的系列,您可以开始 此处。You don't need to override the finalizer in your object, which you have shown in your second code sample by
~MyClass()
.You will need to implement the
IDisposable
pattern. You haven't been explicit in your question if you are using managed and unmanaged resources, but here's a quick sample for a managed resource. Stackoverflow has a myriad of examples on this. Reed Copsey also has a good series on it, and you can start here.