为什么要派代表? [英] Why Delegates?
问题描述
我断断续续地工作了三年,但是我无法确定为什么委托有用.暂时忽略事件,甚至可能忽略向委托添加多个方法的功能(前提是它们都具有相同的签名),为什么要编写代码:
Off and on for three years, I''ve tried and tried, but I cannot wrap my head around why a delegate is useful. Ignoring Events for the moment, and possibly even the ability to add multiple methods to a delegate (provided they all have the same signature), why would you code:
public delegate void SomeDelegate(int x);
SomeDelegate sd = SomeDelegate(SomeMethod);
sd(24);
代替:
Instead of:
SomeMethod(24);
我不明白我已经读过文章,书籍,而且我一定会错过一些相关的内容,因为这似乎浪费了很多代码.
提前谢谢!
副标题和文字,代码块固定. -OriginalGriff [/edit]
I don''t get it. I''ve read articles, books, and I must be missing something relevant because it seems like an awful waste of code.
Thanks in advance!
[edit]Shouty title and text, code block fixed. - OriginalGriff[/edit]
推荐答案
我写了一个技巧/窍门,说明使用与事件无关的委托的一种方法. >
代表很好,对你也很好 [ ^ ]
您还可以使用委托与其他线程的UI线程进行通信.
I wrote a tip/trick about one way to use delegates that doesn''t have anything to do with events.
Delegates Are Good, and Good For You[^]
You also use delegates to communicate with the UI thread from other threads.
我鼓励您阅读以下文章:
http://www.codeproject.com/KB/cs/delegatespart1.aspx [ ^ ]
http://www.codeproject.com/KB/cs/delegatespart2 [ http://www.codeproject.com/KB/cs/delegatespart3 [ http://www.codeproject.com/KB/cs/delegatespart4 [ http://www.codeproject.com/KB/cs/delegatespart5 [
Well I encourage you to read the following articles:
http://www.codeproject.com/KB/cs/delegatespart1.aspx[^]
http://www.codeproject.com/KB/cs/delegatespart2[^]
http://www.codeproject.com/KB/cs/delegatespart3[^]
http://www.codeproject.com/KB/cs/delegatespart4[^]
http://www.codeproject.com/KB/cs/delegatespart5[^]
我的回答-我们为什么需要函数的指针"?
My answer - "why do we need pointers to functions"?
这篇关于为什么要派代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!