<small id='7rkyJ'></small><noframes id='7rkyJ'>

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

        <legend id='7rkyJ'><style id='7rkyJ'><dir id='7rkyJ'><q id='7rkyJ'></q></dir></style></legend>
          <bdo id='7rkyJ'></bdo><ul id='7rkyJ'></ul>
      1. C# - 有人能告诉我为什么以及在哪里应该使用委托吗?

        C# - Can someone tell me why and where I should use delegates?(C# - 有人能告诉我为什么以及在哪里应该使用委托吗?)
        1. <small id='yr7M6'></small><noframes id='yr7M6'>

            <tbody id='yr7M6'></tbody>

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

                  本文介绍了C# - 有人能告诉我为什么以及在哪里应该使用委托吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我认为我将 C# 中委托的概念理解为指向方法的指针,但我找不到任何好的示例来说明在哪里使用它们是个好主意.有哪些例子对于代表来说更优雅/更好,或者不能使用其他方法解决?

                  I think I understand the concept of a delegate in C# as a pointer to a method, but I cant find any good examples of where it would be a good idea to use them. What are some examples that are either significantly more elegant/better with delegates or cant be solved using other methods?

                  推荐答案

                  你说的代表到底是什么意思?以下是它们的两种使用方式:

                  What exactly do you mean by delegates? Here are two ways in which they can be used:

                  void Foo(Func<int, string> f) {
                   //do stuff
                   string s = f(42);
                   // do more stuff
                  }
                  

                  void Bar() {
                   Func<int, string> f = delegate(i) { return i.ToString(); } 
                  //do stuff
                   string s = f(42);
                   // do more stuff
                  }
                  

                  第二个要点是您可以动态声明新函数,作为委托.这可以在很大程度上被 lambda 表达式所取代,并且在您有一小段逻辑想要 1)传递给另一个函数或 2)只是重复执行时很有用.LINQ 就是一个很好的例子.每个 LINQ 函数都将 lambda 表达式作为其参数,指定行为.例如,如果你有一个 List<int>l 然后 l.Select(x=>(x.ToString()) 将对列表中的每个元素调用 ToString().我编写的 lambda 表达式实现为委托.

                  The point in the second one is that you can declare new functions on the fly, as delegates. This can be largely replaced by lambda expressions,and is useful any time you have a small piece of logic you want to 1) pass to another function, or 2) just execute repeatedly. LINQ is a good example. Every LINQ function takes a lambda expression as its argument, specifying the behavior. For example, if you have a List<int> l then l.Select(x=>(x.ToString()) will call ToString() on every element in the list. And the lambda expression I wrote is implemented as a delegate.

                  第一个案例展示了如何实现 Select.您将委托作为参数,然后在需要时调用它.这允许调用者自定义函数的行为.再次以 Select() 为例,函数本身保证了你传递给它的委托会在列表中的每个元素上被调用,并且每个元素的输出都会被返回.该委托实际上做什么取决于您.这使它成为一个非常灵活和通用的功能.

                  The first case shows how Select might be implemented. You take a delegate as your argument, and then you call it when needed. This allows the caller to customize the behavior of the function. Taking Select() as an example again, the function itself guarantees that the delegate you pass to it will be called on every element in the list, and the output of each will be returned. What that delegate actually does is up to you. That makes it an amazingly flexible and general function.

                  当然,它们也用于订阅事件.简而言之,委托允许您引用函数,将它们用作函数调用中的参数,将它们分配给变量以及您喜欢做的任何其他事情.

                  Of course, they're also used for subscribing to events. In a nutshell, delegates allow you to reference functions, using them as argument in function calls, assigning them to variables and whatever else you like to do.

                  这篇关于C# - 有人能告诉我为什么以及在哪里应该使用委托吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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() 和泛型:错误绑定到目标方法)
                    <bdo id='zmsCl'></bdo><ul id='zmsCl'></ul>
                    <legend id='zmsCl'><style id='zmsCl'><dir id='zmsCl'><q id='zmsCl'></q></dir></style></legend>

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

                        <tfoot id='zmsCl'></tfoot>

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