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

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

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

        手动创建委托与使用 Action/Func 委托

        Creating delegates manually vs using Action/Func delegates(手动创建委托与使用 Action/Func 委托)
        <i id='c20QE'><tr id='c20QE'><dt id='c20QE'><q id='c20QE'><span id='c20QE'><b id='c20QE'><form id='c20QE'><ins id='c20QE'></ins><ul id='c20QE'></ul><sub id='c20QE'></sub></form><legend id='c20QE'></legend><bdo id='c20QE'><pre id='c20QE'><center id='c20QE'></center></pre></bdo></b><th id='c20QE'></th></span></q></dt></tr></i><div id='c20QE'><tfoot id='c20QE'></tfoot><dl id='c20QE'><fieldset id='c20QE'></fieldset></dl></div>
            <bdo id='c20QE'></bdo><ul id='c20QE'></ul>

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

              <tbody id='c20QE'></tbody>

            • <tfoot id='c20QE'></tfoot>

                <legend id='c20QE'><style id='c20QE'><dir id='c20QE'><q id='c20QE'></q></dir></style></legend>
                  本文介绍了手动创建委托与使用 Action/Func 委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  今天我正在考虑宣布这一点:

                  Today I was thinking about declaring this:

                  private delegate double ChangeListAction(string param1, int number);
                  

                  但为什么不使用这个:

                  private Func<string, int, double> ChangeListAction;
                  

                  或者如果 ChangeListAction 没有我可以使用的返回值:

                  or if ChangeListAction would have no return value I could use:

                  private Action<string,int> ChangeListAction;
                  

                  那么使用 delegate 关键字声明委托的优势在哪里?

                  so where is the advantage in declaring a delegate with the delegate keyword?

                  是因为 .NET 1.1,而 .NET 2.0 出现了 Action<T>,而 .NET 3.5 出现了 Func<T>?

                  Is it because of .NET 1.1, and with .NET 2.0 came Action<T> and with .NET 3.5 came Func<T>?

                  推荐答案

                  优点是清晰.通过为类型指定一个明确的名称,读者可以更清楚地了解它的作用.

                  The advantage is clarity. By giving the type an explicit name it is more clear to the reader what it does.

                  它还会在您编写代码时为您提供帮助.像这样的错误:

                  It will also help you when you are writing the code. An error like this:

                  cannot convert from Func<string, int, double> to Func<string, int, int, double>
                  

                  没有那么有用:

                  cannot convert from CreateListAction to UpdateListAction
                  

                  这也意味着,如果你有两个不同的委托,它们都采用相同类型的参数,但在概念上做了两件完全不同的事情,编译器可以确保你不会意外地使用你想要的另一个.

                  It also means that if you have two different delegates, both of which take the same types of parameters but conceptually do two entirely different things, the compiler can ensure that you can't accidentally use one where you meant the other.

                  这篇关于手动创建委托与使用 Action/Func 委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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() 和泛型:错误绑定到目标方法)
                1. <tfoot id='CVaCx'></tfoot>
                        <tbody id='CVaCx'></tbody>
                    1. <small id='CVaCx'></small><noframes id='CVaCx'>

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

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