订单处理的基类订单

由网友('终须一日龙抬头)分享简介:在派生类中重写处置应基类Dipose先调用前的派生类的任何本地资源处置?When a derived class overrides Dispose should the base class Dipose be called first before the derived class disposes of an...

在派生类中重写处置应基类Dipose先调用前的派生类的任何本地资源处置?

When a derived class overrides Dispose should the base class Dipose be called first before the derived class disposes of any local resources?

我想问的原因是因为有人对我们的团队说是每一个场景,我想工作,如果这是一个好主意,或只是货物邪教节目,我没有强烈的观点无论哪种方式

The reason I ask is because someone on our team is saying yes for every scenario, I'm trying to work out if this is good idea or just 'cargo cult' programming, I don't have a strong view either way.

 public override void Dispose()
 {
     base.Dispose();

     if (!_isDisposed)
     {
         _isDisposed = true;

         if (_foo != null)
         {
             _foo.Dispose();
         }
     }
 }

 public override void Dispose()
 {
     if (!_isDisposed)
     {
         _isDisposed = true;

         if (_foo != null)
         {
             _foo.Dispose();
         }
     }

     base.Dispose();
 }

注:我不是在寻找如何实现基本处置模式,但更多的澄清,从人民经历

Note: I'm not looking for how to implement the basic dispose pattern but more clarification from peoples experiences.

推荐答案

这取决于

您不能有这样的硬和快速的规则,因为为了你需要调用Dispose将取决于类的实现。

You can't have a hard-and-fast rule for this, because the order you need to call dispose will depend on the implementation of the classes.

有时候,你可能希望它在开始时,有时会底,有时在中间。在大多数情况下,它可能会有问题。

Sometimes you might want it at the start, sometimes at the end, and sometimes in the middle. Most of the time, it probably won't matter.

一般来说,人们似乎第一次调用它(在没有任何其他原因在不同的时间调用它)。

Generally speaking, people seem to call it first (in the absence of any other reason to call it at a different time).

一个理由倾向于调用它首先是,然后派生类有机会做特别的东西之后,如果需要的地方。

One reason to lean towards calling it first is that then the derived class has a chance to do special stuff afterwards if it needs to.

阅读全文

相关推荐

最新文章