委托

转载自:http://www.cnblogs.com/LipeiNet/p/4694225.html

      http://www.cnblogs.com/akwwl/p/3232679.html

在C#委托现在有4种方式,分别是 delegate、Action、Func、Predicate

模拟一下场景:小明最近学习情绪高涨,以前买的书已经满足不了欲望,打算去买本(一个程序员的自我修养)。可是呢以前总是跑书厂买,nm,太远了扛不住,就去跑去附近书店去买,小明去给钱就弄了一本书回来,这个过程就是委托。 

1、delegate  注:Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。

    class Program
    {
        //① 小明要买一本一个程序员自我修养的书籍(xx书就不买)硬性要求 (这就是要定义委托性质)
        private delegate void BuyBook();

        static void Main(string[] args)
        {
            //③ 小明和书店建立关系(给委托绑定方法) 
            var buybook = new BuyBook(Book);
            //④ 小明给钱拿书(触发)
            buybook();
            Console.ReadLine();
        }
        //② 附近书店 (委托的方法)
        public static void Book()
        {
            Console.WriteLine("我是提供书籍的");
        }
    }

2、Action 注:Action至少0个参数,至多16个参数,无返回值。

① 小明很是苦恼,我就是买一本书籍,每次都让我定义下,烦死了,有没有一种方法不去定义委托呢,那么有吗,还真有,就是我们今天讲的Action

var bookAction = new Action(Book);
bookAction();

② 小明现在又不满意了,我把一个程序员的自我修养看完了,现在呢想买本其他书,那怎么办,我是不是要重新再次定义委托。其实不需要你只需要把参数穿过来就可以了。下面我们看Action<T>的用法

    class Program
    {
        static void Main(string[] args)
        {
            var bookAction = new Action<string>(Book);
            bookAction("百年孤独");
            Console.ReadLine();
        }

        public static void Book(string bookName)
        {
            Console.WriteLine("我是买书的是:{0}", bookName);
        }
    }

③ 现在小明又改变主意了,我不仅要自己选择书籍,我还要在一个牛逼的书籍厂家买,有没有这种方式呢,那么告诉你有,Action<in T1,in T2>

    class Program
    {
        static void Main(string[] args)
        {
            var bookAction = new Action<string, string>(Book);
            bookAction("百年孤独", "北京大书店");
            Console.ReadLine();
        }

        public static void Book(string bookName, string changJia)
        {
            Console.WriteLine("我是买书的是:{0}来自{1}", bookName, changJia);
        }
    }

3、Func 注:Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void

小明又发生疑问了,每次我自己都去书店去拿书,有没有一种方法直接送到我家里呢,那么Func专门提供了这样的服务

Func 解释 封装一个不定具有参数(也许没有)但却返回 TResult 参数指定的类型值的方法。

① 我们先看一个没有参数只有返回值的方法

    class Program
    {
        static void Main(string[] args)
        {
            var retBook = new Func<string>(FuncBook);
            Console.WriteLine(retBook());
            Console.ReadLine();
        }

        public static string FuncBook()
        {
            return "送书来了";
        }
    }  

② 有参数有返回值的方法

    class Program
    {
        static void Main(string[] args)
        {
            var retBook = new Func<string,string>(FuncBook);
            Console.WriteLine(retBook("送你一本岛上书店"));
            Console.ReadLine();
        }

        public static string FuncBook(string bookName)
        {
            return bookName;
        }
    }

③ 方法中使用委托返回值

    class Program
    {
        static void Main(string[] args)
        {
            //方式1
            Func<string> funcValue = () => "我是即将传递的值3";
            DisPlayValue(funcValue); 
            //方式2
            DisPlayValue(() => "我是即将传递的值5");
            Console.ReadLine();
        }

        private static void DisPlayValue(Func<string> func)
        {
            var retFunc = func();
            Console.WriteLine("我在测试一下传过来值:{0}", retFunc);
        }
    }

4、Predicate(谓词) 注:Predicate有且只有一个参数,返回值固定为bool

using System.Drawing;

class Program
    {
        static void Main(string[] args)
        {
            Point[] points = { new Point(100, 200), 
            new Point(150, 250), new Point(250, 375), 
            new Point(275, 395), new Point(295, 450) };
            Point first = Array.Find(points, ProductGT10);
            Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
            Console.ReadLine();
        }

        private static bool ProductGT10(Point p)
        {
            if (p.X * p.Y > 100000)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

总结:

    Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型

  Func可以接受0个至16个传入参数,必须具有返回值

  Action可以接受0个至16个传入参数,无返回值

  Predicate只能接受一个传入参数,返回值为bool类型

 

原文地址:https://www.cnblogs.com/ideacore/p/6902510.html