C#中委托的目的是什么 [英] what is the purpose of delegates in C#

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

问题描述

所以我才刚刚开始学习C#,并意识到C#的实际必要性.
请为我提供一个C#示例,以使该示例演示可以由委托完成但不能通过函数调用完成的操作,例如以下

So i just started studying C# and realised the actual necessity of C#.
Please provide me an example in C# such that the example demonstrates something which can be done by a delegate but cannot be done by a function call something like the following

public void ReplicateDeligate(int x,int y)
   {
   Fun1(x,y);//defined elsewhere (May be a normal method,class obj etc)
   Fun2(x,y);//Defined elsewhere
   MyClass.StatClassFun(x,y);

   //.
   //.
   //Etc and so on
   }



我想可以由代表来完成.那有什么区别.
使用委托是否可以在减少内存消耗的情况下提高执行速度? ??

[edit]已添加代码块-OriginalGriff [/edit]



Which i guess can be done by the delegate. So what is the difference.
Does using delegates give better execution speed with less memory consumption? ??

[edit]Code block added - OriginalGriff[/edit]

推荐答案

您应该只具有 http://msdn.microsoft .com/en-us/library/ms173171(v = vs.80).aspx [
You should just have googled[^] it.

Seeing your sample I would suggest you to learn from http://msdn.microsoft.com/en-us/library/ms173171(v=vs.80).aspx[^]


例如,您可能想看一下我的文章:动态方法分发器 [用于线程通信和内部交互的简单阻塞队列线程调用 [传递以N个参数为参数的方法在C# [ ^ ],
C#.NET中的委托的优点是什么? [ ^ ],
对代表的困惑 [
For example, you may want to see my article: Dynamic Method Dispatcher[^]. In particular, see section 6 which shows something not resolved elegantly enough with other methods.

Another short article Tips/Trick article is Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^], see the code sample where queue type is a delegate type.

All this has nothing to do with performance of memory consumption (which are quite good). Delegate is additional abstraction tool adding a lot of flexibility compared to older versions of OOP before introduction of delegates.

Please see also the following interesting discussions (with my answers, with some examples):
Pass a method with N params as parameter in C#[^],
What are the advantages of delegates in C#.NET?[^],
Confusion about delegates[^].

—SA


Delegates 101 - Part I: What is a Delegate?[^]

This is the best solution ever i seen.


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

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