种类
Action,Func,Predicate
区别
Action 无返回值
Func 有返回值,
Predicate 只有一个参数,且返回固定布尔值
注:Func 返回布尔值,且只有一个参数时,与 Predicate等价
代码
public class Test {
//一个string 类型参数
private readonly Action<string> _action;
//一个string类型参数,int 类型返回值
private readonly Func<string,int> _func;
//string 类型参数
private readonly Predicate<string> _predicate;
public Test(Action<string> action,Func<string,int> func,Predicate<string> predicate){
_action += action;
_func += func;
_predicate += predicate;
}
public void Exce(string name){
_action?.Invoke(name);
_func?.Invoke(name);
_predicate?.Invoke(name);
}
}
public class Main(){
var test = new Test(s=>{
//TODO
},s=>{
//TODO
return 1;
},s=> s=="hello");
test.Exce("hello");
}
委托可通过"=" 绑定,也可以通过"+=" 绑定
可通过"+=" 绑定多个委托,按绑定顺序执行
1608

被折叠的 条评论
为什么被折叠?



