<bdo id='EoCF7'></bdo><ul id='EoCF7'></ul>
    <tfoot id='EoCF7'></tfoot>

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

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

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

      代言人有什么好处?

      时间:2023-11-11
      <i id='kZ0T6'><tr id='kZ0T6'><dt id='kZ0T6'><q id='kZ0T6'><span id='kZ0T6'><b id='kZ0T6'><form id='kZ0T6'><ins id='kZ0T6'></ins><ul id='kZ0T6'></ul><sub id='kZ0T6'></sub></form><legend id='kZ0T6'></legend><bdo id='kZ0T6'><pre id='kZ0T6'><center id='kZ0T6'></center></pre></bdo></b><th id='kZ0T6'></th></span></q></dt></tr></i><div id='kZ0T6'><tfoot id='kZ0T6'></tfoot><dl id='kZ0T6'><fieldset id='kZ0T6'></fieldset></dl></div>

      • <legend id='kZ0T6'><style id='kZ0T6'><dir id='kZ0T6'><q id='kZ0T6'></q></dir></style></legend>

          • <tfoot id='kZ0T6'></tfoot>
              <bdo id='kZ0T6'></bdo><ul id='kZ0T6'></ul>
                <tbody id='kZ0T6'></tbody>

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

              1. 本文介绍了代言人有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                使用委托有什么好处/优势?谁能提供一些简单的例子?

                What are the benefits/advantages of using delegates? Can anyone provide any simple examples?

                推荐答案

                它们是封装一段代码的好方法.例如,当您将事件处理程序附加到按钮时,该处理程序就是委托.按钮不需要知道它的作用,只需要知道如何在正确的时间调用它.

                They're a great way of encapsulating a piece of code. For instance, when you attach an event handler to the button, that handler is a delegate. The button doesn't need to know what it does, just how to call it at the right time.

                另一个例子是 LINQ——过滤、投影等都需要相同类型的模板代码;所有的变化是表示过滤器、投影等的逻辑.使用 C# 3 中的 lambda 表达式(转换为委托或表达式树),这使它变得非常简单:

                Another example is LINQ - filtering, projecting etc all require the same kind of template code; all that changes is the logic to represent the filter, the projection etc. With lambda expressions in C# 3 (which are converted into delegates or expression trees) this makes it really simple:

                var namesOfAdults = people.Where(person => person.Age >= 18)
                                          .Select(person => person.Name);
                

                (这也可以表示为查询表达式,但我们不要离委托太远.)

                (That can also be represented as a query expression, but let's not stray too far from delegates.)

                委托的另一种思考方式是作为单方法接口类型.例如,EventHandler 委托类型有点像:

                Another way of thinking of a delegate is as a single-method interface type. For example, the EventHandler delegate type is a bit like:

                public interface IEventHandler
                {
                    void Invoke(object sender, EventArgs e)
                }
                

                但框架中的委托支持允许将委托链接在一起、异步调用、用作事件处理程序等.

                But the delegate support in the framework allows delegates to be chained together, invoked asynchronously, used as event handlers etc.

                有关代表和事件的更多信息,请参阅我关于该主题的文章.它的重点是事件,但也包括委托.

                For more on delegates and events, see my article on the topic. Its focus is events, but it covers delegates too.

                这篇关于代言人有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:简单委托(委托)与多播委托 下一篇:lambda 表达式的意义何在?

                相关文章

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

              4. <small id='U90CL'></small><noframes id='U90CL'>

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