C#委托 Delegate

来源:互联网 发布:有毒网络剧歌曲 编辑:程序博客网 时间:2024/06/05 16:07
    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);        }    }



0 0
原创粉丝点击