代表的用途是什么,我们在哪些情况下使用它们 [英] what are the uses of delegates and in which cases do we use them

查看:152
本文介绍了代表的用途是什么,我们在哪些情况下使用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人帮我解决这个问题

Please anyone help me to solve this problem

推荐答案

在我的项目中,我在接下来的3种情况下一直在使用代表:



1.首次使用代理是指定义自己的事件

有关详细信息,请参阅下一页链接。 [ ^ ]



2.因为委托与C中的函数指针类似,你可以像使用函数指针一样使用它们,例如假设你想要实现一个通用的排序,你可以把它作为所用排序方法的委托的最后一个参数,这个参数可以用几种排序方法代替。



3.You可以在接口和类中具有delegate类型的属性,并以这种方式动态设置将用于代替它们的方法。
In my projects I have been using delegates in the next 3 situations:

1.First usage of delegates are when you are defining your own events.
See next link for details.[^]

2.Because delegates are similar with pointer to functions from C you could use them in the same manner like pointer to functions are used, for example supposing that you want to implement a generic sorting you could have as last parameter the delegate to the sort method used, and this parameter could be replaced by several sorting methods.

3.You could have properties of type delegate in your interfaces and your classes and in this way to dynamically setup methods that will be used in place of them.


参考:

1. C#代表的权力 [ ^ ]

2. 代表和活动的重要用途 [ ^ ]
Refer:
1. The Power of Delegates in C#[^]
2. 6 important uses of Delegates and Events[^]


看到这个和你在一起将会了解:为什么我们使用代表 [ ^ ]
See this and you will get to know : Why we use Delegates[^]


这篇关于代表的用途是什么,我们在哪些情况下使用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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