1、委托(delegate)和Func
Func:.NET框架中使用 delegate定义的泛型委托。类似还有Action , Predicate ,NET框架中源码如下:public delegate void Action(T obj);
public delegate TResult Func(T arg);
public delegate bool Predicate(T obj);
委托(delegate):使用delegate关键字来自定义委托。public delegate void MyDelegate(int x);
2、使用委托(delegate)作为方法返回类型//定义委托
public delegate int callback(int i);
//能正常返回callback委托
public callback Success()
{
Func f = x => x;
//return new test(f.Invoke);
return f.Invoke; // 返回 Func类型,需要使用f.Invoke,相当于return new callback(f.Invoke);
}
//错误写法
public callback Fail()
{
Func f = x => x;
return f; // 不能直接返回,因为是两个委托,会编译错误
}
相关文档: