为什么我们在C#中使用委托代替函数 [英] Why will we use delegate over function in C#

查看:88
本文介绍了为什么我们在C#中使用委托代替函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用委托和功能执行相同的操作。那么我们将在哪里使用委托以及我们在哪里使用函数。

解决方案

粗略地说委托就像一个函数指针,这是间接<的一种方式/ i>调用方法。

例如,请参阅我对另一个问题的解决方案:如何使用循环调用多个方法,请帮助 [ ^ ]。

更多细节,例如,在这里: oop - 我在哪里使用代表? - 堆栈溢出 [ ^ ]。


然后可以将委托对象传递给可以调用引用方法的代码,而无需在编译时知道将调用哪个方法。



代表特别用于实现事件和回调方法。


函数是常态。使用功能不起作用的代理,例如此处

We can perform same thing using delegate and function. So Where will we use delegate and where will we use function.

解决方案

Roughly speaking a delegate is like a function pointer, that is a way to indirectly call a method.
As an example, see my solution to another question: "how to call multiple methods using loop, please help"[^].
More details, for instance, here: oop - Where do I use delegates? - Stack Overflow[^].


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.

Delegates are especially used for implementing events and the call-back methods.


A function is the norm. Use delegates where a function doesn't work, like here.


这篇关于为什么我们在C#中使用委托代替函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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