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

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

      • <bdo id='Vmazy'></bdo><ul id='Vmazy'></ul>
      <tfoot id='Vmazy'></tfoot><legend id='Vmazy'><style id='Vmazy'><dir id='Vmazy'><q id='Vmazy'></q></dir></style></legend>
      1. Func 之间的区别使用委托和 lambda 表达式

        Difference between Funclt;gt; with delegate and lambda expression(Func 之间的区别使用委托和 lambda 表达式)

      2. <tfoot id='dUjV9'></tfoot>
        <legend id='dUjV9'><style id='dUjV9'><dir id='dUjV9'><q id='dUjV9'></q></dir></style></legend>
            <tbody id='dUjV9'></tbody>

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

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

              1. <i id='dUjV9'><tr id='dUjV9'><dt id='dUjV9'><q id='dUjV9'><span id='dUjV9'><b id='dUjV9'><form id='dUjV9'><ins id='dUjV9'></ins><ul id='dUjV9'></ul><sub id='dUjV9'></sub></form><legend id='dUjV9'></legend><bdo id='dUjV9'><pre id='dUjV9'><center id='dUjV9'></center></pre></bdo></b><th id='dUjV9'></th></span></q></dt></tr></i><div id='dUjV9'><tfoot id='dUjV9'></tfoot><dl id='dUjV9'><fieldset id='dUjV9'></fieldset></dl></div>
                  本文介绍了Func 之间的区别使用委托和 lambda 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在深入了解 C# 的更高级功能时,我遇到了一些代码,但我并不完全了解它们的区别.就是这两行:

                  while deepening myself to more advanced features of C#, I came across some code, which I didn't exactly know the difference of. It's about these two lines:

                  Func<string, int> giveLength = (text => text.Length);
                  

                  Func<string, int> giveLength = delegate(string text) { return text.Length; };
                  

                  这可以用同样的方式:

                  Console.WriteLine(giveLength("A random string."));
                  

                  所以基本上..这两行有什么区别?这些行是否编译为相同的 CIL?

                  So basically.. What is the difference of these two lines? And are these lines compiling to the same CIL?

                  推荐答案

                  它们基本上是一样的.它们都是 C# 规范术语中的匿名函数.

                  They're the same, basically. They're both anonymous functions in C# specification terminology.

                  Lambda 表达式一般比较简洁,也可以转换成表达式树,这对于进程外 LINQ 至关重要.

                  Lambda expressions are generally more concise, and can also be converted to expression trees, which are crucial for out-of-process LINQ.

                  如果您不关心,匿名方法允许您删除参数列表.例如:

                  Anonymous methods allow you to drop the parameter list if you don't care. For example:

                  EventHandler handler = delegate { 
                      Console.WriteLine("Sender and args don't matter");
                  };
                  

                  鉴于后一点很少需要,匿名方法正在成为现代 C# 中的濒危物种.Lambda 表达式更为常见.

                  Given how rarely the latter point is required, anonymous methods are becoming an endangered species in modern C#. Lambda expressions are much more common.

                  这篇关于Func 之间的区别使用委托和 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() 和泛型:错误绑定到目标方法)
                • <legend id='oSmnD'><style id='oSmnD'><dir id='oSmnD'><q id='oSmnD'></q></dir></style></legend>

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

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

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