C#常见委托のdelegate定义,Func,Action,Predicate总结

委托,顾名思义,就是让其他代理,本质就是为具有共性方法组定义一个方法模板;(交流可以加qq群:568055323)

委托常见的方式有一般委托显示定义,Func<T,TResult> (T,表示传入参数的类型,TResult)表示返回的类型,可以最多有四个参数传入,

Action<T>,与Func的唯一区别是没有返回值;Predicate <T>就比较多单一了, 只能传入一个参数,然后返回一个bool值。

下面是示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FuncTest
{
    class Program
    {
        // 委托声明 -- 定义一个签名:
       delegate double MathAction(double num);
       static double Double(double input)
       {
          return input* 2;
       }
        static void Main(string[] args)
        {
            //***********************普通委托
            //使用一个方法实例化委托
            MathAction ma = Double;
            double result = ma(4.5);
            Console.WriteLine(result.ToString());

            //匿名方法
            MathAction ma2 = delegate(double input) 
            {
                return input * 3;
            };
            Console.WriteLine(ma2(3));

            //lambda表达式
            MathAction ma3 = s => s * s * s;
            Console.WriteLine(ma3(2));

            //**********************Func<T,Tresult>委托
            //利用Func,不用再显示定义委托
            Func<double, double> ma4 = Double;
            Console.WriteLine(ma4(5).ToString());

            //利用Func,匿名方法
            Func<double,double> ma5 = delegate (double input) 
            {
                return input * input;
            };
            Console.WriteLine(ma5(5));

            //利用Func,lambda表达式
            Func<double, double> ma6 = s => s * s * s;
            Console.WriteLine(ma6(5));

            //和其他表达式结合
            // 声明了一个Func委托类型的变量selector并用Lambda表达式进行实例化 
            // 这个Lambda表达式将用来获取一个字符串并将这个字符串转化为大写并返回
            Func<string, string> selector = str => str.ToUpper();

            // 创建一个字符串数组
            string[] words = { "orange", "apple", "Article", "elephant" };
            // 依次遍历这个字符串数组并调用委托实例selector进行处理
            IEnumerable<String> aWords = words.Select(selector);
  
            // 输出结果到控制台
            foreach (String word in aWords)
                      Console.WriteLine(word);
            //**********************Action<T> 委托,与Func<T,TResult>唯一的区别就是没有返回值

            Action<string> ma7 = delegate (string input)
            {
                Console.WriteLine(input);
            };
            ma7("from Action<string>");

            //***********************Predicate<T>, 形式只有一个,传入一个参数,返回一个bool值
            Predicate<string[]> predicate = delegate (string[] x)
            {
                var r = from p in x
                             where p.Contains("s")
                             select p;
                if (r.ToList().Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            };
            string[] _value = { "charlies", "nancy", "alex", "jimmy", "selina" };

            if (predicate(_value))
            {
                Console.WriteLine("They contain.");
            }
            else
            {
                Console.WriteLine("They don't contain.");
            }


            Console.ReadLine();


        }



    }
}
原文地址:https://www.cnblogs.com/xietianjiao/p/7095296.html