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); } }