什么是委托呢? [英] What are delegates for?

查看:176
本文介绍了什么是委托呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  什么是代表的优势是什么?

我创建了一个示例应用程序。我真的发现很难理解为什么要使用委托,因为没有代表我们就可以实现一切。

I have created a sample application. I am really finding it difficult to understand why to use delegates because without delegates we can achieve everything .

class Program
{
    public delegate double Integrand(double x);
    static double MyFunc1(double x) { return x + 10; }
    static double MyFunc2(double x) { return x + 20; }

    public static double Gauss3DelMethod(Integrand f)
    {
        double a = f(1);
        return a;
    }

    public static double Gauss3SimpleMethod(double x)
    {
        double a = x;
        return a;
    }

    static void Main(string[] args)
    {
        //with delegates
        double res = Gauss3DelMethod(MyFunc1);
        double res1 = Gauss3DelMethod(MyFunc2);

        //without delegates
        double res2 = Gauss3SimpleMethod(MyFunc1(1));
        double res3 = Gauss3SimpleMethod(MyFunc2(1));

        int i = 0;
    }
}

那么,我为什么要使用委托?

So, why should I use delegates?

推荐答案

在你的特殊的例子,也许是的没有的重要。 (目前还不清楚它正试图实现的。)

In your particular example, maybe it's not important. (It's not clear what it's trying to achieve.)

但是,假设你想使该方法的几个不同的输入执行相同的 MyFunc1 MyFunc2 。例如,假设您正在执行的牛顿迭代法,工作在普通的功能。在这种情况下,你不能调用函数的一次的,并传递到方法 - 你想传递的的实际功能的进入方法,这样的方法可以调用它与任何输入需要

But suppose you wanted to make the method execute the same MyFunc1 or MyFunc2 for several different inputs. For example, suppose you were implementing the Newton-Raphson method, to operate over a general function. In that situation, you couldn't call the function once and pass that into the method - you want to pass the actual function into the method, so that the method can call it with whatever inputs it needs.

有一个类似的例子是排序。例如,使用LINQ可以写这样的:

A similar example is sorting. For example, using LINQ you can write something like:

var sorted = people.OrderBy(x => x.Age).ToList();

目前,我们正在传递一个函数来预测每个源元素的排序键。我们没有执行该功能我们自己 - 排序依据会为我们做(懒洋洋)

There we're passing a function to project each source element to the ordering key. We don't have to execute that function ourselves - OrderBy will do it for us (lazily).

当然,所有这一切都可以用单一方法接口做得太 - 代表和单方法接口有很多共同点。不过,代表们有过单方法接口具有以下优点:

Of course, all of this can be done with single-method interfaces too - delegates and single-method interfaces have a lot in common. However, delegates have the following benefits over single-method interfaces:

  • 委托是多播 - 它们可以组合/删除,通常为求的事件
  • 您可以异步的BeginInvoke / EndInvoke会执行它们
  • 您可以用匿名方法和lambda EX pressions构建它们
  • 您可以经由前pression树数据使用lambda EX presions重新present逻辑;然后这些可被编译为代表
  • 您可以用适当的签名的任何方法构建一个委托 - 因此,例如一个类可以有多个方法实施相同的委托类型,而不能在一个班
  • 同样下放的实施方法可以是私有的,而接口的方法必须是公共的(或有点公开通过显式接口实现)
  • Delegates are multi-cast - they can be combined/removed, usually for the sake of events
  • You can execute them asynchronously with BeginInvoke/EndInvoke
  • You can build them with anonymous methods and lambda expressions
  • You can represent logic as data via expression trees using lambda expresions; these can then be compiled into delegates
  • You can build a delegate from any method with the appropriate signature - so for example a single class can have multiple methods "implementing" the same delegate type, whereas you can't implement an interface multiple times in one class
  • Likewise delegate implementation methods can be private, whereas interface methods have to be public (or somewhat-public via explicit interface implementation)

所有这些可能已经解决不同使用单方法接口,当然,但代表是一个方便的替代。

All of these could have been addressed differently using single-method interfaces, of course, but delegates are a handy alternative.

结论:委托​​是非常有用的。仅仅因为你已经写了一些琐碎的code这并不特别需要它们并不意味着他们是没有用的。我强烈建议你看看LINQ,事件处理程序和第三方物流,所有这些都使用委托严重。

In conclusion: delegates are very useful. Just because you've written some trivial code which doesn't particularly require them doesn't mean they're not useful. I would strongly suggest you look into LINQ, event handlers and the TPL, all of which use delegates heavily.

这篇关于什么是委托呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆