1. <small id='dRhXf'></small><noframes id='dRhXf'>

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

    <tfoot id='dRhXf'></tfoot>
    1. 如何在 C# 中清除事件订阅?

      时间:2023-11-11
          1. <i id='AYZ3q'><tr id='AYZ3q'><dt id='AYZ3q'><q id='AYZ3q'><span id='AYZ3q'><b id='AYZ3q'><form id='AYZ3q'><ins id='AYZ3q'></ins><ul id='AYZ3q'></ul><sub id='AYZ3q'></sub></form><legend id='AYZ3q'></legend><bdo id='AYZ3q'><pre id='AYZ3q'><center id='AYZ3q'></center></pre></bdo></b><th id='AYZ3q'></th></span></q></dt></tr></i><div id='AYZ3q'><tfoot id='AYZ3q'></tfoot><dl id='AYZ3q'><fieldset id='AYZ3q'></fieldset></dl></div>
              <tbody id='AYZ3q'></tbody>

              <bdo id='AYZ3q'></bdo><ul id='AYZ3q'></ul>
              • <small id='AYZ3q'></small><noframes id='AYZ3q'>

                <legend id='AYZ3q'><style id='AYZ3q'><dir id='AYZ3q'><q id='AYZ3q'></q></dir></style></legend>
              • <tfoot id='AYZ3q'></tfoot>
              • 本文介绍了如何在 C# 中清除事件订阅?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                参加以下 C# 课程:

                Take the following C# class:

                c1 {
                 event EventHandler someEvent;
                }
                

                如果有很多订阅 c1someEvent 事件并且我想将它们全部清除,那么实现此目的的最佳方法是什么?还要考虑到此事件的订阅可能/是 lambdas/匿名委托.

                If there are a lot of subscriptions to c1's someEvent event and I want to clear them all, what is the best way to achieve this? Also consider that subscriptions to this event could be/are lambdas/anonymous delegates.

                目前我的解决方案是向 c1 添加一个 ResetSubscriptions() 方法,将 someEvent 设置为 null.我不知道这是否有任何看不见的后果.

                Currently my solution is to add a ResetSubscriptions() method to c1 that sets someEvent to null. I don't know if this has any unseen consequences.

                推荐答案

                在类中,您可以将(隐藏)变量设置为 null.空引用是有效地表示空调用列表的规范方式.

                From within the class, you can set the (hidden) variable to null. A null reference is the canonical way of representing an empty invocation list, effectively.

                在课堂之外,您不能这样做 - 事件基本上会公开订阅"和取消订阅",仅此而已.

                From outside the class, you can't do this - events basically expose "subscribe" and "unsubscribe" and that's it.

                值得注意的是,类似字段的事件实际上在做什么——它们同时创建了一个变量一个事件.在类中,您最终会引用该变量.从外部,您引用该事件.

                It's worth being aware of what field-like events are actually doing - they're creating a variable and an event at the same time. Within the class, you end up referencing the variable. From outside, you reference the event.

                有关详细信息,请参阅我的关于事件和代表的文章.

                See my article on events and delegates for more information.

                这篇关于如何在 C# 中清除事件订阅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:C# 泛型不允许委托类型约束 下一篇:Control.BeginInvoke 的 MethodInvoker 与 Action

                相关文章

              • <small id='ixFmc'></small><noframes id='ixFmc'>

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

                2. <legend id='ixFmc'><style id='ixFmc'><dir id='ixFmc'><q id='ixFmc'></q></dir></style></legend>

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

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