订单处理的基类 [英] Order of disposal for base classes

查看:122
本文介绍了订单处理的基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在派生类中重写处置应基类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.

这篇关于订单处理的基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆