您的位置:首页 > 编程语言 > C#

C#委托 Delegate

2015-11-02 22:24 513 查看
class Program
{
static void Main(string[] args)
{
DelegateFather[] handers =
{
new Delegate(),
new AnonymousDelgate(),
new LambdaDelgate(),
new FuncDelgate()
};
foreach(var hander in handers)
{
var result = hander.DelegatFunction(10);
Console.WriteLine(result);
}

Console.ReadKey();
}
}
public abstract class DelegateFather
{
public const int num = 100;
public delegate bool DelegatMethod(int a);
public abstract bool DelegatFunction(int a);
}
public class Delegate:DelegateFather
{
public override bool DelegatFunction(int a)
{
DelegatMethod hander = CompareMethod;

return hander(a);
}

public bool CompareMethod(int a)
{
return a > num;
}
}
public class AnonymousDelgate:DelegateFather
{
public override bool DelegatFunction(int m)
{
DelegatMethod hander = delegate (int a) { return a > num; };
return hander(m);
}
}
public class LambdaDelgate:DelegateFather
{
public override bool DelegatFunction(int m)
{
DelegatMethod del = a => a > num;
return del(m);
}
}
public class FuncDelgate:DelegateFather
{
public override bool DelegatFunction(int m)
{
Func<int, bool> hander = a => a > num;
return hander(m);
}
}




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: