<legend id='LDxWo'><style id='LDxWo'><dir id='LDxWo'><q id='LDxWo'></q></dir></style></legend>

        <tfoot id='LDxWo'></tfoot>

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

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

        在对象初始化器中分配事件

        时间:2023-11-11

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

              <tbody id='5z7r9'></tbody>
              <bdo id='5z7r9'></bdo><ul id='5z7r9'></ul>

              <small id='5z7r9'></small><noframes id='5z7r9'>

                  <tfoot id='5z7r9'></tfoot>
                  本文介绍了在对象初始化器中分配事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  为什么不能在 C# 的对象初始值设定项中分配事件和属性?这样做似乎很自然.

                  Why isn't it possible to assign events along with properties in object initializers in C#? It seems to be so natural to do so.

                  var myObject = new MyClass()
                       {
                          Property = value,
                          Event1 = actor,
                          // or
                          Event2 += actor
                       };  
                  

                  或者有什么我不知道的技巧?

                  Or is there some trick that I don't know of?

                  推荐答案

                  就外部合约而言,事件没有setter,只有addremove methods - 订阅者可以从事件中注册和注销,publishing 对象通过引发"事件来决定何时调用回调.因此,分配事件"的想法通常是没有意义的.

                  As far the external contract is concerned, an event doesn't have a setter, only add and remove methods - subscribers can register and unregister from the event, and the publishing object decides when to invoke the callbacks by 'raising' the event. Consequently, the idea of "assigning an event", in general, is meaningless.

                  然而,当你在一个类中声明一个事件时,C# 编译器为你提供了一个真正方便的特性:当你不提供自己的实现时,它会创建一个private,为您提供支持委托字段,以及适当的添加/删除实现.这允许您在类内而不是在类外设置事件"(实际上是支持字段).要理解这一点,请考虑:

                  However, when you declare an event in a class, the C# compiler provides you with what is really a convenience-feature: when you don't provide your own implementation, it creates a private, backing delegate-field for you, along with the appropriate add / remove implementations . This allows you to "set the event" (really the backing field) within the class, but not outside it. To understand this, consider:

                  public class Foo
                  {
                      // implemented by compiler
                      public event EventHandler MyEvent;
                  
                      public static Foo FooFactory(EventHandler myEventDefault)
                      {
                         // setting the "event" : perfectly legal
                         return new Foo { MyEvent = myEventDefault }; 
                      }
                  }
                  
                  public class Bar
                  {
                      public static Foo FooFactory(EventHandler myEventDefault)
                      {
                          // meaningless: won't compile
                          return new Foo { MyEvent = myEventDefault };
                      }
                  }
                  
                  
                  public class Baz
                  {
                      // custom implementation
                      public event EventHandler MyEvent
                      {      
                          add { }  // you can imagine some complex implementation here
                          remove { } // and here
                      }
                  
                      public static Baz BazFactory(EventHandler myEventDefault)
                      {
                          // also meaningless: won't compile
                          return new Baz { MyEvent = myEventDefault };
                      }
                  }
                  

                  这篇关于在对象初始化器中分配事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:EndInvoke() 是可选的,是可选的,还是绝对不是可选的? 下一篇:什么是 Func,如何以及何时使用

                  相关文章

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

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

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

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