• <bdo id='z8ktm'></bdo><ul id='z8ktm'></ul>

    <small id='z8ktm'></small><noframes id='z8ktm'>

      <i id='z8ktm'><tr id='z8ktm'><dt id='z8ktm'><q id='z8ktm'><span id='z8ktm'><b id='z8ktm'><form id='z8ktm'><ins id='z8ktm'></ins><ul id='z8ktm'></ul><sub id='z8ktm'></sub></form><legend id='z8ktm'></legend><bdo id='z8ktm'><pre id='z8ktm'><center id='z8ktm'></center></pre></bdo></b><th id='z8ktm'></th></span></q></dt></tr></i><div id='z8ktm'><tfoot id='z8ktm'></tfoot><dl id='z8ktm'><fieldset id='z8ktm'></fieldset></dl></div>

    1. <legend id='z8ktm'><style id='z8ktm'><dir id='z8ktm'><q id='z8ktm'></q></dir></style></legend>

    2. <tfoot id='z8ktm'></tfoot>

        为什么总是需要在具有 IDisposable 成员的对象上实现 IDisposable?

        Why is it always necessary to implement IDisposable on an object that has an IDisposable member?(为什么总是需要在具有 IDisposable 成员的对象上实现 IDisposable?)
      1. <i id='hvTW9'><tr id='hvTW9'><dt id='hvTW9'><q id='hvTW9'><span id='hvTW9'><b id='hvTW9'><form id='hvTW9'><ins id='hvTW9'></ins><ul id='hvTW9'></ul><sub id='hvTW9'></sub></form><legend id='hvTW9'></legend><bdo id='hvTW9'><pre id='hvTW9'><center id='hvTW9'></center></pre></bdo></b><th id='hvTW9'></th></span></q></dt></tr></i><div id='hvTW9'><tfoot id='hvTW9'></tfoot><dl id='hvTW9'><fieldset id='hvTW9'></fieldset></dl></div>

          <tfoot id='hvTW9'></tfoot>

          1. <legend id='hvTW9'><style id='hvTW9'><dir id='hvTW9'><q id='hvTW9'></q></dir></style></legend>
                <tbody id='hvTW9'></tbody>

              <small id='hvTW9'></small><noframes id='hvTW9'>

                  <bdo id='hvTW9'></bdo><ul id='hvTW9'></ul>

                  本文介绍了为什么总是需要在具有 IDisposable 成员的对象上实现 IDisposable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  据我所知,如果你有一个类 A 有一个成员 m 是 IDisposable,A 应该实现 IDisposable 并且它应该在其中调用 m.Dispose() 是一个公认的规则.

                  From what I can tell, it is an accepted rule that if you have a class A that has a member m that is IDisposable, A should implement IDisposable and it should call m.Dispose() inside of it.

                  我找不到令人满意的理由.

                  I can't find a satisfying reason why this is the case.

                  我理解如果你有非托管资源,你应该提供一个终结器以及 IDisposable 的规则,这样如果用户没有显式调用 Dispose,终结器仍然会在 GC 期间清理.

                  I understand the rule that if you have unmanaged resources, you should provide a finalizer along with IDisposable so that if the user doesn't explicitly call Dispose, the finalizer will still clean up during GC.

                  但是,有了该规则,您似乎不需要拥有该问题所涉及的规则.比如……

                  However, with that rule in place, it seems like you shouldn't need to have the rule that this question is about. For instance...

                  如果我有课:

                  class MyImage{
                    private Image _img;
                    ... }
                  

                  约定规定我应该有 MyImage : IDisposable.但是如果Image遵循约定,实现了finalizer,我不关心资源的及时释放,那还有什么意义呢?

                  Conventions states that I should have MyImage : IDisposable. But if Image has followed conventions and implemented a finalizer and I don't care about the timely release of resources, what's the point?

                  更新

                  在 这里.

                  推荐答案

                  但如果 Image 遵循约定并实现了一个终结器,我不在乎及时发布资源,有什么用?

                  But if Image has followed conventions and implemented a finalizer and I don't care about the timely release of resources, what's the point?

                  那么没有一个,如果你不关心及时释放,你可以确保一次性对象写得正确(事实上我从来没有做过这样的假设,即使是 MSs 代码.你从来没有知道什么时候有东西意外溜过).关键是你应该关心,因为你永远不知道它什么时候会导致问题.考虑一个开放的数据库连接.让它闲置,意味着它不会在池中被替换.如果您有多个请求进入一个,您可能会用完.

                  Then there isn't one, if you don't care about timely release, and you can ensure that the disposable object is written correct (in truth I never make an assumption like that, not even with MSs code. You never know when something accidentally slipped by). The point is that you should care, as you never know when it will cause a problem. Think about an open database connection. Leaving it hanging around, means that it isn't replaced in the pool. You can run out if you have several requests come in for one.

                  没有什么说如果你不在乎就必须这样做.这样想,就像在非托管程序中释放变量一样.您不必这样做,但这是非常可取的.如果没有其他原因,从程序继承的人不必想知道为什么它没有得到照顾,然后尝试清理它.

                  Nothing says you have to do it if you don't care. Think of it this way, it's like releasing variables in an unmanaged program. You don't have to, but it is highly advisable. If for no other reason the person inheriting from the program doesn't have to wonder why it wasn't taken care of and then try and clear it up.

                  这篇关于为什么总是需要在具有 IDisposable 成员的对象上实现 IDisposable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                      <bdo id='LAJsi'></bdo><ul id='LAJsi'></ul>
                        <tbody id='LAJsi'></tbody>

                      <small id='LAJsi'></small><noframes id='LAJsi'>

                      • <legend id='LAJsi'><style id='LAJsi'><dir id='LAJsi'><q id='LAJsi'></q></dir></style></legend>

                        1. <tfoot id='LAJsi'></tfoot>

                          • <i id='LAJsi'><tr id='LAJsi'><dt id='LAJsi'><q id='LAJsi'><span id='LAJsi'><b id='LAJsi'><form id='LAJsi'><ins id='LAJsi'></ins><ul id='LAJsi'></ul><sub id='LAJsi'></sub></form><legend id='LAJsi'></legend><bdo id='LAJsi'><pre id='LAJsi'><center id='LAJsi'></center></pre></bdo></b><th id='LAJsi'></th></span></q></dt></tr></i><div id='LAJsi'><tfoot id='LAJsi'></tfoot><dl id='LAJsi'><fieldset id='LAJsi'></fieldset></dl></div>