代表的目的 [英] The purpose of delegates

查看:118
本文介绍了代表的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重复:




事件和代理之间的差异及其各自的应用程序



代表的优势是什么?



我在哪里使用代理?


我不知道代表的目的是什么。我没有太多使用它们,并没有真正想到的东西。



在我的课程中,它写成一个代表是所有方法的蓝色打印遵守其签名。



此外,您可以向一个代理添加多个方法,然后按照添加的顺序在彼此之后执行。对于那些不会返回任何值的局部变量或方法的方法来说,这可能只是有用的。



我已经读过C#实现了作为代理的事件作为:

  //摘要:表示
将处理没有事件$ b $的事件的方法b数据。

//参数:

//发件人:事件的来源。

// e:不包含事件数据的System.EventArgs。

[Serializable]

[ComVisible(true)]

public delegate void EventHandler(object sender,EventArgs e);

仍然有点混乱。有人可以给出这个概念的好的,有用的例子吗?

解决方案

是的,


$ b $你几乎在那里代表是指要调用的方法或函数。 .NET使用事件来表示..当有人按下此按钮时,我希望您执行这段代码。



例如,在使用GPS应用程序时:

  public delegate void PositionReceivedEventHandler(double latitude,double longitude); 

这表示该方法必须采用两个双精度作为输入,并返回void。当我们来定义事件时:

  public event PositionReceivedEventHandler PositionReceived; 

这意味着PositionRecieved事件调用一个与
相同定义的方法PositionReceivedEventHandler我们定义的代表。所以当你做

  PositionRecieved + = new PositionReceivedEventHandler(method_Name); 

method_Name必须与委托一致,以便我们知道如何执行该方法,它期待什么参数。如果您使用Visual Studio设计器将某些事件添加到按钮中,例如,它将对预期对象和EventArgs参数的委托进行处理。



希望有助于一些...


Duplicate:

Difference between events and delegates and its respective applications

What are the advantages of delegates?

Where do I use delegates?

I wonder what the purpose of delegates is. I haven't used them that much and can't really think of something.

In my courses, it's written that a delegate is a blue-print for all methods that comply with its signature.

Also, you can add multiple methods to one delegate, and then they'll be executed after eachother in the order they were added. Which is probably only usefull for methods that affect local variables or methodes that don't return any values.

I've read that C# implements Events as delegates, which is documented as being:

//Summary: Represents the method that
will handle an event that has no event
data.

//Parameters:

//sender: The source of the event.

//e: An System.EventArgs that contains no event data.

[Serializable]

[ComVisible(true)] 

public delegate void EventHandler(object sender, EventArgs e);

Still, it's kinda confusing. Can someone give a good, usefull example of this concept?

解决方案

Yeah,

You're almost there. A delegate refers to a method or function to be called. .NET uses the Events to say.. when someones presses this button, I want you to execute this piece of code.

For example, in the use of a GPS application:

public delegate void PositionReceivedEventHandler(double latitude, double longitude);

This says that the method must take two doubles as the inputs, and return void. When we come to defining an event:

public event PositionReceivedEventHandler PositionReceived;  

This means that the PositionRecieved event, calls a method with the same definition as the PositionReceivedEventHandler delegate we defined. So when you do

PositionRecieved += new PositionReceivedEventHandler(method_Name);

The method_Name must match the delegate, so that we know how to execute the method, what parameters it's expecting. If you use a Visual Studio designer to add some events to a button for example, it will all work on a delegate expecting an object and an EventArgs parameter.

Hope that helps some...

这篇关于代表的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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