代表们,为什么呢? [英] Delegates, Why?

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

问题描述

可能显示的文件:结果
  你什么时候在C#中使用委托?结果
  代表的目的

我看到关于使用代表的许多问题。我仍然不清楚的地方以及你为什么要使用委托而不是直接调用的方法。

I have seen many question regarding the use of delegates. I am still not clear where and WHY would you use delegates instead of calling the method directly.

我听到这句话很多次:然后委托对象可以被传递到code可调用引用的方法,而不必知道在该方法将被调用编译时

I have heard this phrase many times: "The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked."

我不明白这种说法多么正确。

I don't understand how that statement is correct.

我写了下面的例子。比方说,你有3种方法具有相同的参数:

I've written the following examples. Let's say you have 3 methods with same parameters:

   public int add(int x, int y)
    {
        int total;
        return total = x + y;
    }
    public int multiply(int x, int y)
    {
        int total;
        return total = x * y;
    }
    public int subtract(int x, int y)
    {
        int total;
        return total = x - y;
    }

现在我宣布一个委托:

public delegate int Operations(int x, int y);

现在我可以把它一步一个申报的处理程序使用该委托(或您的代理直接)

Now I can take it a step further a declare a handler to use this delegate (or your delegate directly)

电话委托:

MyClass f = new MyClass();

Operations p = new Operations(f.multiply);
p.Invoke(5, 5);

或处理函数调用

f.OperationsHandler = f.multiply;
//just displaying result to text as an example
textBoxDelegate.Text = f.OperationsHandler.Invoke(5, 5).ToString();

在这两种情况下,我看到被指定我的倍增的方法。为什么人们会使用短语在运行时更改功能或以上的人吗?

In these both cases, I see my "multiply" method being specified. Why do people use the phrase "change functionality at runtime" or the one above?

为什么要使用委托,如果我每次申报委托时,它需要指向的方法?如果它需要指向的方法,为什么不直接调用该方法?在我看来,我必须编写更多的code使用委托不仅仅是直接使用的功能。

Why are delegates used if every time I declare a delegate, it needs a method to point to? and if it needs a method to point to, why not just call that method directly? It seems to me that I have to write more code to use delegates than just to use the functions directly.

有人可以给我一个现实世界的情况?我完全糊涂了。

Can someone please give me a real world situation? I am totally confused.

推荐答案

在运行时更改的功能是不是代表完成。

Changing functionality at runtime is not what delegates accomplish.

基本上,代表们为您节省打字的crapload。

Basically, delegates save you a crapload of typing.

例如:

class Person
{
    public string Name { get; }
    public int Age { get; }
    public double Height { get; }
    public double Weight { get; }
}

IEnumerable<Person> people = GetPeople();

var orderedByName = people.OrderBy(p => p.Name);
var orderedByAge = people.OrderBy(p => p.Age);
var orderedByHeight = people.OrderBy(p => p.Height);
var orderedByWeight = people.OrderBy(p => p.Weight);

在上面的code时, P =&GT; p.Name P =&GT; p.Age 等都是拉姆达前pressions计算结果为 Func键&LT;人,T&GT; 代表(其中 T 字符串 INT 双击双击,分别)。

In the above code, the p => p.Name, p => p.Age, etc. are all lambda expressions that evaluate to Func<Person, T> delegates (where T is string, int, double, and double, respectively).

现在让我们考虑我们如何已经取得了上述的没有的代表。而不是的排序依据方法采取委托参数,我们将不得不抛弃泛型和定义这些方法:

Now let's consider how we could've achieved the above without delegates. Instead of having the OrderBy method take a delegate parameter, we would have to forsake genericity and define these methods:

public static IEnumerable<Person> OrderByName(this IEnumerable<Person> people);
public static IEnumerable<Person> OrderByAge(this IEnumerable<Person> people);
public static IEnumerable<Person> OrderByHeight(this IEnumerable<Person> people);
public static IEnumerable<Person> OrderByWeight(this IEnumerable<Person> people);

这将完全吸。我的意思是,首先,code已经成为它仅适用于人的集合无限可重复使用较少键入。此外,我们需要复制并粘贴同样的code四次,只改变1或2行中的每个副本(其中人员的相关属性引用 - - 否则会看起来都一样)!这将很快成为一个难以维护的混乱。

This would totally suck. I mean, firstly, the code has become infinitely less reusable as it only applies to collections of the Person type. Additionally, we need to copy and paste the very same code four times, changing only 1 or 2 lines in each copy (where the relevant property of Person is referenced -- otherwise it would all look the same)! This would quickly become an unmaintainable mess.

所以代表让你做出你的code更可重复使用的<​​strong>和更易于维护被抽象掉code中的某些行为,可以切换进出。

So delegates allow you to make your code more reusable and more maintainable by abstracting away certain behaviors within code that can be switched in and out.

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

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