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

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

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

        由 C# 中的 lambda 创建的委托的生命周期是多少?

        What is the lifetime of a delegate created by a lambda in C#?(由 C# 中的 lambda 创建的委托的生命周期是多少?)
        <tfoot id='p2sdK'></tfoot>
          <tbody id='p2sdK'></tbody>

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

                • 本文介绍了由 C# 中的 lambda 创建的委托的生命周期是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Lambda 很好,因为它们提供 简洁性和局部性 和 另一种封装形式.不必编写仅在一次可以使用 lambda 时使用的函数.

                  Lambdas are nice, as they offer brevity and locality and an extra form of encapsulation. Instead of having to write functions which are only used once you can use a lambda.

                  虽然想知道它们是如何工作的,但我直觉地认为它们可能只创建一次.这启发了我创建一个解决方案,允许 限制通过使用 lambda 作为创建它的范围的标识符,将类成员的范围超出私有到一个特定范围.

                  While wondering how they worked, I intuitively figured they are probably only created once. This inspired me to create a solution which allows to restrict the scope of a class member beyond private to one particular scope by using the lambda as an identifier of the scope it was created in.

                  这个实现工作,虽然可能有点矫枉过正(仍在研究它),证明我的假设是正确的.

                  This implementation works, although perhaps overkill (still researching it), proving my assumption to be correct.

                  一个较小的例子:

                  class SomeClass
                  {
                      public void Bleh()
                      {
                          Action action = () => {};
                      }
                  
                      public void CallBleh()
                      {
                          Bleh();  // `action` == {Method = {Void <SomeClass>b__0()}}
                          Bleh();  // `action` still == {Method = {Void <SomeClass>b__0()}}
                      }
                  }
                  

                  lambda 会返回一个新实例,还是保证始终相同?

                  Would the lambda ever return a new instance, or is it guaranteed to always be the same?

                  推荐答案

                  根据您在这里的问题和您对 Jon 的回答的评论,我认为您混淆了很多事情.为了确保清楚:

                  Based on your question here and your comment to Jon's answer I think you are confusing multiple things. To make sure it is clear:

                  • 为给定 lambda 支持委托的方法始终相同.
                  • 允许在词法上出现两次的相同"lambda 的委托支持方法是相同的,但实际上不是em> 在我们的实现中也是如此.
                  • 为给定 lambda 创建的 delegate 实例 可能始终相同,也可能不同,这取决于编译器缓存它的智能程度.
                  • The method that backs the delegate for a given lambda is always the same.
                  • The method that backs the delegate for "the same" lambda that appears lexically twice is permitted to be the same, but in practice is not the same in our implementation.
                  • The delegate instance that is created for a given lambda might or might not always be the same, depending on how smart the compiler is about caching it.

                  所以如果你有类似的东西:

                  So if you have something like:

                  for(i = 0; i < 10; ++i)
                      M( ()=>{} )
                  

                  那么每次调用 M 时,你都会得到委托的相同的实例,因为编译器很聪明并且会生成

                  then every time M is called, you get the same instance of the delegate because the compiler is smart and generates

                  static void MyAction() {}
                  static Action DelegateCache = null;
                  
                  ...
                  for(i = 0; i < 10; ++i)
                  {
                      if (C.DelegateCache == null) C.DelegateCache = new Action ( C.MyAction )
                      M(C.DelegateCache);
                  }
                  

                  如果你有

                  for(i = 0; i < 10; ++i)
                      M( ()=>{this.Bar();} )
                  

                  然后编译器生成

                  void MyAction() { this.Bar(); }
                  ...
                  for(i = 0; i < 10; ++i)
                  {
                      M(new Action(this.MyAction));
                  }
                  

                  您每次都会使用相同的方法获得一个新的委托.

                  You get a new delegate every time, with the same method.

                  编译器被允许生成(但实际上此时并没有)

                  The compiler is permitted to (but in fact does not at this time) generate

                  void MyAction() { this.Bar(); }
                  Action DelegateCache = null;
                  ...
                  for(i = 0; i < 10; ++i)
                  {
                      if (this.DelegateCache == null) this.DelegateCache = new Action ( this.MyAction )
                      M(this.DelegateCache);
                  }
                  

                  在这种情况下,如果可能,您将始终获得相同的委托实例,并且每个委托都将由相同的方法支持.

                  In that case you would always get the same delegate instance if possible, and every delegate would be backed by the same method.

                  如果你有

                  Action a1 = ()=>{};
                  Action a2 = ()=>{};
                  

                  然后在实践中编译器将其生成为

                  Then in practice the compiler generates this as

                  static void MyAction1() {}
                  static void MyAction2() {}
                  static Action ActionCache1 = null;
                  static Action ActionCache2 = null;
                  ...
                  if (ActionCache1 == null) ActionCache1 = new Action(MyAction1);
                  Action a1 = ActionCache1;
                  if (ActionCache2 == null) ActionCache2 = new Action(MyAction2);
                  Action a2 = ActionCache2;
                  

                  但是编译器被允许检测到两个 lambda 是相同的并生成

                  However the compiler is permitted to detect that the two lambdas are identical and generate

                  static void MyAction1() {}
                  static Action ActionCache1 = null;
                  ...
                  if (ActionCache1 == null) ActionCache1 = new Action(MyAction1);
                  Action a1 = ActionCache1;
                  Action a2 = ActionCache1;
                  

                  现在清楚了吗?

                  这篇关于由 C# 中的 lambda 创建的委托的生命周期是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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() 和泛型:错误绑定到目标方法)

                    <tbody id='Praj4'></tbody>
                  <legend id='Praj4'><style id='Praj4'><dir id='Praj4'><q id='Praj4'></q></dir></style></legend>
                • <small id='Praj4'></small><noframes id='Praj4'>

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