22. Delegates

本文介绍了C#中委托的概念及其工作原理。委托允许程序员创建指向方法的引用,并且能够调用这些方法,这为实现回调和事件处理等功能提供了基础。文章强调了委托与C++等语言中的函数指针的不同之处,同时展示了其在匿名调用方面的灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[Note: Delegates enable scenarios that other languages?such as C++, Pascal,
and Modula?have addressed with
function pointers. Unlike C++ function pointers, however, delegates are
fully object oriented, and unlike
C++ pointers to member functions, delegates encapsulate both an object
instance and a method. end note]
A delegate declaration defines a class that is derived from the class
System.Delegate. A delegate instance
encapsulates one or more methods, each of which is referred to as a
callable entity. For instance methods, a
callable entity consists of an instance and a method on that instance. For
static methods, a callable entity consists
of just a method. Given a delegate instance and an appropriate set of
arguments, one can invoke all of that
delegate instance?s methods with that set of arguments.
[Note: An interesting and useful property of a delegate instance is that it
does not know or care about the classes
of the methods it encapsulates; all that matters is that those methods be
compatible (§22.1) with the delegate?s
type. This makes delegates perfectly suited for ?anonymous? invocation. end
note]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值