c# 委托

委托是一个方法指针

1.声明一个委托类型 

public  delegate T DelegateMethod(Paras);

2.一个要使用的方法 

public T Method(Paras) {messageBox.show();}

3.实例一个委托变量 

DelegateMethod s=Method;
//或
DelegateMethod s=new DelegateMethod(Method);

4.执行

s(paras);

注意事项:委托方法的声明返回值类型和参数均要与所指方法类型和参数一致。

类似c:

int *p;

int a=8;

*p=&a;


组合委托:

一个委托绑定多个方法,可以取消绑定或增加绑定。

s= MethodA+MethodB;

s+=MethodA;

s-=MethodA;

匿名方法:

DelegateMethod s=delegate(paras){messageBox.Show();};

Lambda表达式:

=>lambda运算符,读作goes to

DelegateMethod s=(paras)=>{messageBox.Show();};//无返回值的方法

进一步简化了匿名方法委托


DelegateMethod s=(int paras)=>paras++; //如果方法只有一个返回值,则可简化大括号和return;

DelegateMethod s=(paras)=>paras++; //如果方法只有一个返回值,则可简化大括号和return;参数类型列表可以写也可以不写;

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值