<legend id='im9Kn'><style id='im9Kn'><dir id='im9Kn'><q id='im9Kn'></q></dir></style></legend>
  • <tfoot id='im9Kn'></tfoot>

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

      • <bdo id='im9Kn'></bdo><ul id='im9Kn'></ul>
    2. <small id='im9Kn'></small><noframes id='im9Kn'>

        使用 Action.Invoke 是否被认为是最佳实践?

        Is using Action.Invoke considered best practice?(使用 Action.Invoke 是否被认为是最佳实践?)

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

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

                • <tfoot id='gBFDf'></tfoot>
                  <legend id='gBFDf'><style id='gBFDf'><dir id='gBFDf'><q id='gBFDf'></q></dir></style></legend>
                    <tbody id='gBFDf'></tbody>

                • 本文介绍了使用 Action.Invoke 是否被认为是最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我有以下代码,我应该调用 Action 还是调用 Action.Invoke?

                  If I have the below code, should I just call the Action or should it call Action.Invoke?

                  public class ClassA
                  {
                    public event Action<string> OnAdd;
                  
                    private void SomethingHappened()
                    {
                      if (OnAdd != null)
                       OnAdd("It Happened"); //Should it be OnAdd.Invoke("It Happened") ???????
                    }
                  }
                  
                  public class ClassB
                  {
                  
                    public ClassB()
                    {
                      var myClass = new ClassA();
                      myClass.OnAdd += Add;
                    }
                  
                    private void Add(string Input)
                    {
                      //do something
                    }  
                  }
                  

                  推荐答案

                  两者是等价的,编译器将OnAdd("It Happened");转换成OnAdd.Invoke("It发生了"); 为您服务.

                  The two are equivalent, the compiler converts OnAdd("It Happened"); into OnAdd.Invoke("It Happened"); for you.

                  我想这是一个偏好问题,但我个人更喜欢简洁的形式.

                  I guess it's a matter of preference, however I personally prefer the terser form.

                  顺便说一句,通常最好在调用类级别委托之前获取类级别委托的本地副本以避免竞争条件,即 OnAdd 在检查时不为空,但是在它被调用的时候:

                  As an aside, it is generally preferable to take a local copy of a class level delegate before invoking it to avoid a race condition whereby OnAdd is not null at the time that it is checked, but is at the time that it is invoked:

                  private void SomethingHappened()
                  {
                    Action<string> local = OnAdd;
                    if (local != null)
                    {
                      local("It Happened");
                    }
                  }
                  

                  这篇关于使用 Action.Invoke 是否被认为是最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                • <legend id='UMl4M'><style id='UMl4M'><dir id='UMl4M'><q id='UMl4M'></q></dir></style></legend>
                      <bdo id='UMl4M'></bdo><ul id='UMl4M'></ul>

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

                        <tbody id='UMl4M'></tbody>
                          <tfoot id='UMl4M'></tfoot>

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