Difference betweeen event and delegate

本文探讨了事件和委托在软件开发中的区别。事件是对象发送的消息,用于通知接收者某些动作的发生,而委托则是一种类型安全的函数指针,可以持有对特定签名方法的引用。事件总是在定义它们的对象上被调用,而委托则可以在任何地方调用它们所引用的方法。
摘要由CSDN通过智能技术生成

Difference betweeen event and delegate is 

1. Event : An event is a message sent by an object to signal the occurrence of an action. The action could be caused by user interaction, such as a mouse click, or it could be triggered by some other program logic. The object that raises (triggers) the event is called the event sender. The object that captures the event and responds to it is called the event receiver.

    1).  An event is not a type . 

    2).  An event since its not a type can be declared in an interface.

    3).  An event is always invoked only on the object of the class where the event is defined/declared. So the method raised as part of the event should always be declared in the class that contains that object.

   2. Delegate :  A delegate is a class that can hold a reference to a method. Unlike other classes, a delegate class has a signature, and it can hold references only to methods that match its signature. A delegate is thus equivalent to a type-safe function pointer or a callback. While delegates have other uses, the discussion here focuses on the event handling functionality of delegates. The following example shows an event delegate declaration.


    1).  A Delegate is a type.

    2).  A Delegate since is a type cannot be declared in an interface.

    3).  As long as the reference of a method is attached to the delegate it can call that method regardless of its location.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值