我在哪里使用委托? [英] Where do I use delegates?

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

问题描述

现实世界中有哪些需要代表的地方?我很好奇这种方法是最佳解决方案的情况或模式.无需代码.

What are some real world places that call for delegates? I'm curious what situations or patterns are present where this method is the best solution. No code required.

推荐答案

委托是定义特定类型方法的命名类型.正如类定义为它定义的给定类型的对象布置所有成员一样,委托也为它定义的类型的方法布置方法签名.

A delegate is a named type that defines a particular kind of method. Just as a class definition lays out all the members for the given kind of object it defines, the delegate lays out the method signature for the kind of method it defines.

根据这个陈述,委托是一个函数指针,它定义了该函数的外观.

Based on this statement, a delegate is a function pointer and it defines what that function looks like.

委托的真实世界应用的一个很好的例子是谓词.在链接的示例中,您会注意到 Array.Find 需要要搜索的数组,然后是一个谓词来处理要查找的内容的条件.在这种情况下,它传递一个与 Predicate 签名匹配的方法 ProductGT10.

A great example for a real world application of a delegate is the Predicate. In the example from the link, you will notice that Array.Find takes the array to search and then a predicate to handle the criteria of what to find. In this case it passes a method ProductGT10 which matches the Predicate signature.

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

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